【问题标题】:Calling two different custom cell in one UITableView issue在一个 UITableView 问题中调用两个不同的自定义单元格
【发布时间】:2013-07-17 23:18:48
【问题描述】:

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

我在谷歌上搜索了自定义单元格,我使用了我必须在下面的代码中使用的方式,但没有任何反应。

这是我的 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;
}

【问题讨论】:

  • 你连接插座了吗?
  • 这不是处理 2 种不同细胞类型的正确方法。在这里查看我的答案:stackoverflow.com/questions/17711452/…。这处理集合视图,但它与表视图相同。
  • @Wain 你说的网点是什么意思?
  • 您只返回“单元格”。那么'cell1'呢?
  • 是的,表格视图和集合视图的api非常相似,检索不同单元格(或集合视图中的项目)的方式是相同的。

标签: iphone ios uitableview custom-cell


【解决方案1】:

你需要做这样的事情:

- (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;
    }
}

我可能将 if 条件倒置,所以请仔细检查。

【讨论】:

  • cellForRowAtIndexPath 需要返回一个 UITableViewCell 实例。您的代码将抛出错误,因为您没有返回 if-else 语句之外的任何内容。
  • @Sebyddd 不,没关系。
  • 刚试了一下,'控件可能到达非空函数的末尾。' :)
  • @Sebyddd 只要 if 部分和 else 部分都有返回值,就不会出现编译器错误。
  • 我知道你在说什么。这可能是编译器中的错误。它不应该抛出这个错误。
【解决方案2】:

这个实现:

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

导致单元格具有一些访问器方法但没有初始化实例。您需要实现init 方法来创建实例或将变量(您定义为出口)连接到关联的XIB 文件。阅读this guide

我也同意 cmets 关于如何创建不同单元实例的结构。它非常杂乱无章,您似乎混淆了参考。您应该将内容拆分为不同的方法,或者至少将用于创建单元格的代码放在 if 语句中,这样您就不能输入单元格实例名称。

【讨论】:

    【解决方案3】:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    { if (indexPath.row == 0) //not 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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多