gpt4 book ai didi

ios - UITableView - 初始加载时未着色的单元格?

转载 作者:行者123 更新时间:2023-11-29 11:04:00 25 4
gpt4 key购买 nike

我正在我的 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 在我看到任何东西之前就被触发了,< strong>and 我的着色条件被击中,导致......没有着色。

解决方法是在对 TableView 调用 reloadData 后循环访问可见单元格,我只是希望有更简单的方法。

丹麦人

最佳答案

单元格着色需要在 tableView:willDisplayCell:forRowAtIndexPath: 委托(delegate)方法中完成,而不是 tableView:cellForRowAtIndexPath: 方法。

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

关于ios - UITableView - 初始加载时未着色的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14487382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com