2013-08-20 51 views
2

我有一个2个部分的分组表。更改重用单元格的类型?

第一部分只有1个单元,它是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; 
} 
+0

编辑为向您显示我的总体布局 – JMD

+0

您是否为InspectionMasterTableViewCell.xib中的单元设置了相同的重用标识“InspectionMasterTableViewCell”? –

回答

3

通常,每个类型的细胞应该有它自己的再利用标识符。在cellForRowAtIndexPath中,通过指定适当的重用标识符来请求所需的单元类型,然后转换为正确的类型。

+0

这似乎是我在做什么。然而,每当第一个单元格被重新使用时,出现的单元格都是空白的,就像数据未被正确添加,因为缺少标签 – JMD

+0

@JMD,您在哪里添加数据?我在'cellForRowAtIndexPath'中看不到它。 –