使用 NIB 定义 UITableViewCell Out of Storyboard



我使用 Json 数组来显示来自服务器的数据。

当我将 CellIdentifier 更改为

我在 CellIdentifier 中使用的 @Cell 时,pushDetailView 正在工作并且它会转到下一页,但是当我再次更改为 CustomCell 时,只需在第一页显示城市和州的名称,当我单击它时不会转到下一页。 我需要@CustomCell的原因,因为我在第一页需要 2 个标签视图,当我将其更改为@cell它时显示一个标签。

谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
City * cityObject;
cityObject = [ citiesArry objectAtIndex:indexPath.row];
cell.textLabel.text = cityObject.cityState;
cell.detailTextLabel.text = cityObject.cityPopulation;

cell.detailTextLabel.numberOfLines = 4;

//NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://mobileapps.binaryappdev.com/applelogo.png"]  ];
//[[cell imageView] setImage:[UIImage imageWithData:imageData]  ];

return cell;
}

#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

if ([[segue identifier] isEqualToString:@"pushDetailView"])
{
    NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
    City * object = [citiesArry objectAtIndex: indexPath.row];
    [[segue destinationViewController] getCity:object];
}
}

#pragma mark -
#pragma mark Class Methods

-(void) retrievedData;
{
NSURL * URL = [ NSURL URLWithString:getDataURL];
NSData * data = [ NSData  dataWithContentsOfURL:URL];
jsonArry = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:Nil];

//city

citiesArry = [[NSMutableArray alloc] init];
//loop
for (int i=0; i<jsonArry.count; i++) {
    NSString * cID = [[ jsonArry objectAtIndex:i] objectForKey:@"id"];
    NSString * cName = [[ jsonArry objectAtIndex:i] objectForKey:@"cityName"];
    NSString * cState = [[ jsonArry objectAtIndex:i] objectForKey:@"cityState"];
    NSString * cCountry = [[ jsonArry objectAtIndex:i] objectForKey:@"country"];
    NSString * cPopulation = [[ jsonArry objectAtIndex:i] objectForKey:@"cityPopulation"];
    NSString * cImage = [[ jsonArry objectAtIndex:i] objectForKey:@"cityImage"];


    [citiesArry addObject:[[City alloc] initWithcityName:cName andcityState:cState andcityCountry:cCountry andcityPopulation:cPopulation andcityImage:cImage andcityID:cID ]];


     }
     [self.tableView reloadData];
}
@end

详细视图控制器

@synthesize cityNameLabel, stateLabel, countryLabel, populationLabel, currentCity;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

[self setLabels];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


#pragma mark -
#pragma mark Class Methods

-(void) getCity:(id)cityObject

{

currentCity = cityObject;
}

-(void) setLabels
{
cityNameLabel.text= currentCity.cityName;
countryLabel.text = currentCity.cityCountry;
stateLabel.text = currentCity.cityState;
populationLabel.text = currentCity.cityPopulation;
cityNameLabel.numberOfLines= 0;
}

@end

如果你在推入单元格时没有自定义单元格的segue,你的方法didSelectRow:AtIndexPath将被调用。你可以从该方法或[self performSegueWithIdentifier: @"pushDetailView" sender: self];

相关内容

  • 没有找到相关文章

最新更新