【问题标题】:TableView showing repeated data after some indexTableView 在某些索引后显示重复数据
【发布时间】:2012-03-28 16:27:58
【问题描述】:

我正在使用自定义表格视图单元格和我的表格视图在索引 5 之后显示重复数据我不知道为什么会这样在 ipad 中它显示在索引 8 重复数据之后它可能是索引问题但找不到它下面是代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 静态 NSString *CellIdentifier = @"item";

ItemCell *cell = (ItemCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[ItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(@"indexpath.row is %d",indexPath.row);

NSString *stre = [grouporphoto objectAtIndex:indexPath.row];
if([stre isEqualToString:@"Group"]){
    folderimage = YES;
    NSLog(@"here is folder");

    cell.item = [items objectAtIndex:indexPath.row];


}
else if([stre isEqualToString:@"Photo"]){
    folderimage = NO;
    NSLog(@"here is Photo");

    cell.item = [items objectAtIndex:indexPath.row];


}




return cell;
}

请帮忙 提前致谢

【问题讨论】:

  • 难道你从来没有进入过ifs? cell.item 永远不会更新,这可以解释为什么你会得到重复的数据(重复使用的单元格)。将 NSLog 放在 ifs 之外,看看是否打印。

标签: ios


【解决方案1】:

您应该在另一个 else 语句中重置所有变量,这样当您的单元格被重用时,其中的变量就不会了。

else
{
    cell.item = nil;
}

这应该会为您解决问题。

【讨论】:

  • 谢谢 Totumus,你是对的,我用了这个 else{ cell = nil; cell = [[[ItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; }
【解决方案2】:

这是因为 iOS 处理 TableViews 的方式。一旦一个新的单元格进入视野(从视野之外,即在屏幕底部下方),它会要求一个新的单元格,在这种情况下,它会尝试重用一个已经移出视野的单元格。

由于重用,并非所有实例变量都在单元格上重置。您需要明确更改您想要更改的所有内容。换句话说,您希望在每个单元格上可能不同的所有图片、文本、标签等,每次都明确设置它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2018-01-02
    • 2018-02-17
    相关资源
    最近更新 更多