gpt4 book ai didi

ios - 滚动时单元格背景在 UITableViewCell 中错误地重复

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

我有一个填充 UITableView 的完整列表。我想用不同的颜色为其中一个单元格设置背景,它最初可以工作,但由于某种原因,当我开始上下滚动表格时,它开始绘制更多具有绿色背景的单元格。

请注意,总是一个detailCell.detailTimeLabel.text等于currentTime

我的代码是:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

... SOME MORE CODE HERE ...

if ([detailCell.detailTimeLabel.text isEqualToString:currentTime]) {
UIView* backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backgroundView.backgroundColor = [UIColor greenColor];
detailCell.backgroundView = backgroundView;
for (UIView* view in detailCell.contentView.subviews)
{
view.backgroundColor = [UIColor clearColor];
}
}
}

可能是哪个问题?

最佳答案

您尝试将数据存储在 tableviewcell 中。您不能这样做,因为细胞会不断重复使用。您看到重复出现的背景是因为它的同一个单元格以不同的文本一遍又一遍地显示。

当您将单元格出列时,您需要将其重置为空白并清除所有先前的数据。然后,只有当您放入单元格中的数据 具有当前时间时,您才应该设置背景颜色。

作为一般规则,您永远不应引用单元格中的数据。如果您需要了解特定单元格中的内容,请查看数据模型本身内索引路径中的数据。

关于ios - 滚动时单元格背景在 UITableViewCell 中错误地重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2568936/

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