UITableView - 初始加载时单元格没有着色?

时间:2013-01-23 19:03:05

标签: ios objective-c

我正在VC中实施UITableViewDelegateUITableViewDataSource

我有这段代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
    (NSIndexPath *) indexPath {
    // do stuff 
    if(condition){
        cell.textLabel.backgroundColor = [UIColor redColor];
    }
    else {
        cell.textLabel.backgroundColor = [UIColor blackColor];
    }
}

非常标准的东西。我遇到的问题是第一组可见细胞没有着色。如果我滚动到不可见的单元格,我会得到正确的彩色单元格。如果我向后滚动,我的细胞就会变色。

我知道细胞是根据需要“创造”和“摧毁”的,这解释了其中的一部分,我只是不明白为什么cellForRowAtIndexPath,它在我看到之前被解雇了,我的着色条件被击中,导致......没有着色。

解决方法是在表视图上调用reloadData后迭代可见单元格,我只是希望有一种更简单的方法。

戴恩

1 个答案:

答案 0 :(得分:2)

细胞着色需要在tableView:willDisplayCell:forRowAtIndexPath:委托方法中完成,而不是tableView:cellForRowAtIndexPath:方法。

tableView:cellForRowAtIndexPath:方法中删除任何单元格着色逻辑,并将其移至tableView:willDisplayCell:forRowAtIndexPath:方法。

相关问题