【问题标题】:Change a re-used cell's type? [closed]更改重复使用的单元格的类型? [关闭]
【发布时间】:2013-08-20 19:25:09
【问题描述】:

我有一个包含 2 个部分的分组表。

第一部分只有一个单元格,它是 XIB 的一个特定子类。表格中的其余单元格显示没有 XIB 的基本数据。

我遇到的问题是,当第一个单元格被重新使用时,单元格的子类显然仍然是使用 XIB 的那个,所以当我尝试对其应用数据时,它不会在他们的位置有任何适当的标签等。

我需要要么忽略第一个单元格并继续使用第二种单元格,要么更改单元格的类型。

处理这种情况的最佳方法是什么,你是如何做到的?

试过了

if (cell == nil || [cell isKindOfClass:[InspectionMasterTableViewCell class]]) 但这似乎没有任何效果。

我的 cellForRowAtIndexPath 的基本布局是这样的

if (indexPath.section == InspectionsMasterSectionData)
{
    // CREATE CELL
    static NSString *CellWithIdentifier = @"InspectionMasterTableViewCell";
    InspectionMasterTableViewCell *cell = (InspectionMasterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"InspectionMasterTableViewCell" owner:nil options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }

    return cell;
}
else
{
    static NSString *CellWithIdentifier = @"FormTableViewCell";
    FormTableViewCell *cell = (FormTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil || [cell isKindOfClass:[InspectionMasterTableViewCell class]])
        cell = [[FormTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];

   //CELL DATA
    return cell;
}

【问题讨论】:

  • 已编辑以向您展示我的总体布局
  • 您是否为 InspectionMasterTableViewCell.xib 中的单元格设置了相同的重用标识符“InspectionMasterTableViewCell”?

标签: iphone ios objective-c ipad uitableview


【解决方案1】:

通常,每种类型的单元格都应该有自己的重用标识符。在cellForRowAtIndexPath 中,通过指定正确的重用标识符来询问您想要的单元格类型,然后转换为正确的类型。

【讨论】:

  • 这似乎是我正在做的事情。然而,每次重复使用第一个单元格时,出现的单元格都是空白的,就像由于缺少标签而没有正确添加数据
  • @JMD,你在哪里添加数据?我在您的cellForRowAtIndexPath 上面没有看到它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多