gpt4 book ai didi

ios - 更新 UITableViewCell 内容的正确位置

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

<分区>

我总是像这样更新 tableView:cellForRowAtIndexPath: 中的 UITableViewCell 内容:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell:UITableViewCell = self.resultsTableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as UITableViewCell!

// update cell content
cell.backgroundColor = UIColor.clearColor()
cell.textLabel?.text = myTextString

return cell
}

documentation还显示 tableView:cellForRowAtIndexPath: 中正在更新的内容。然而,今天I read

The content of the cell has to be set right before it is displayed. We don’t care which cell object is being used. All we care about is that it has the right stuff to display when it is passed on to the renderer. So, the right place to set the cell’s content is tableView:willDisplayCell:forRowAtIndexPath:

the same post 中提到了这样做的原因.它谈到了

...problem where the custom cell appears to ‘randomly change content’ when the table is scrolled. The reason why this happens is because the content of the cell is set in the tableView:cellForRowAtIndexPath: method, which is not the right place. In this method, the system expects only to obtain the cell object. Once a cell is displayed, the system will not ask for the cell object until it loses it (i.e. when the cell object is destroyed) and cannot find a replacement to reuse. If, however, a replacement is found, it is reused, and you will end up seeing the contents of that reused cell in place of the original cell.

这样对吗?我以前从未注意到问题,但我应该更新 tableView:willDisplayCell:forRowAtIndexPath: 中的单元格内容吗?我是否被文档误导了?

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