在一个UITableView问题中调用两个不同的自定义单元格

我创build了一个自定义单元格FeatureCell,它有5个图像在主视图中调用的行,但是当我调用它时,我得到空行。 那么请问哪里可能是我的问题?

我已经search了关于自定义单元格,我用下面的代码中使用的方式,但没有发生。

这是我的FeatureCell.h

@interface FeatureCell : UITableViewCell{ IBOutlet UIImageView *img1; IBOutlet UIImageView *img2; } @property (nonatomic, retain) UIImageView *img1; @property (nonatomic, retain) UIImageView *img2; @end 

这是我的FeatureCell.m

 @implementation FeatureCell @synthesize img,img1,img2,img3,img4; @end 

这是我的视图控制器.m

 - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section == 0) { return [objectCollection count]; } else{ return 1; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* cellIdentifier1 = @"FeatureCell"; FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; if (cell1 == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil]; cell1 = (FeatureCell*)[nib objectAtIndex:0]; } static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; } if ([indexPath section] == 0) { containerObject = [objectCollection objectAtIndex:indexPath.row]; [[cell textLabel] setText:containerObject.store]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } else{ cell1.img1.image = [UIImage imageNamed:@"shower.png"]; cell1.img2.image = [UIImage imageNamed:@"parking.png"]; } return cell; } 

你需要做这样的事情:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; } containerObject = [objectCollection objectAtIndex:indexPath.row]; [[cell textLabel] setText:containerObject.store]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } else { static NSString* cellIdentifier1 = @"FeatureCell"; FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; if (cell1 == nil) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil]; cell1 = (FeatureCell*)[nib objectAtIndex:0]; } cell1.img1.image = [UIImage imageNamed:@"shower.png"]; cell1.img2.image = [UIImage imageNamed:@"parking.png"]; return cell1; } } 

我可能有条件倒退,所以仔细检查。

这个实现:

 @implementation FeatureCell @synthesize img,img1,img2,img3,img4; @end 

使用一些存取方法但没有初始化实例的单元格中的结果。 您需要实现一个init方法来创build实例或将variables(您定义为出口)连接到关联的XIB文件。 阅读本指南 。

我也同意关于你如何创build你不同的单元格实例的结构的评论。 这是非常混乱,你似乎在混合引用。 你应该把事情分解成不同的方法,或者至less把代码放在if语句里面,这样你就不能input单元格实例的名字。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ if(indexPath.row == 0) //不是indexPath.section …解决了我的问题

 { static NSString *CellIdentifier = @"MenuHeadingCustomCell"; MenuHeadingCustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell ==nil) { cell = [[MenuHeadingCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } return cell; } else { static NSString* cellIdentifier1 = @"LevelViewCell"; LevelViewCell *cell1 =[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; if (cell1 ==nil) { cell1 = [[LevelViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1]; // row counts which dictionary entry to load: _Dictionary=[_array objectAtIndex:indexPath.row]; cell1.LevelTitleText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelText"]]; cell1.LevelSubText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelSubText"]]; cell1.LevelThumb.image =[UIImage imageNamed:[_Dictionary objectForKey:@"LevelLabelThumb"]]; } return cell1; }