滚动时tableview单元格中的值发生了变化,我在tableview中使用了原型单元格。(values are changing in tableview cell when scrolling a

系统教程 行业动态 更新时间:2024-06-14 16:57:17
滚动时tableview单元格中的值发生了变化,我在tableview中使用了原型单元格。(values are changing in tableview cell when scrolling and i used prototype cell in tableview. my code is below) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! ViewAllCell stringClassObj = stringClassArray[indexPath.row] cell.modelNameLbl.text = stringClassObj.model cell.brandLbl.text = stringClassObj.vehicleTitle cell.fuelTypeLbl.text = stringClassObj.fuelType cell.priceLbl.text = stringClassObj.Price cell.discountLbl.text = "5%" let URLBaseString = "http://vehiclebuzzzz.com/" let url = URL(string:URLBaseString .appending(stringClassObj.vehicleImage!)) DispatchQueue.global().async { let dataurl = try? Data(contentsOf: url!) DispatchQueue.main.async { cell.productImageView.image = UIImage(data: dataurl!) } self.tableViewObj.reloadData() } return cell } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as! ViewAllCell stringClassObj = stringClassArray[indexPath.row] cell.modelNameLbl.text = stringClassObj.model cell.brandLbl.text = stringClassObj.vehicleTitle cell.fuelTypeLbl.text = stringClassObj.fuelType cell.priceLbl.text = stringClassObj.Price cell.discountLbl.text = "5%" let URLBaseString = "http://vehiclebuzzzz.com/" let url = URL(string:URLBaseString .appending(stringClassObj.vehicleImage!)) DispatchQueue.global().async { let dataurl = try? Data(contentsOf: url!) DispatchQueue.main.async { cell.productImageView.image = UIImage(data: dataurl!) } self.tableViewObj.reloadData() } return cell }

最满意答案

原因是您要将数据加载到后台队列中,这需要一段时间,一旦数据可用,就更新单元(在主线程中,这是正确的)。 但是,在数据加载完毕后,您可以上下滚动表格视图,因此单元格消失可能位于顶部,并将在底部重新用于其他数据。 现在背景加载已经完成了finisehd(旧的单元格的图像数据已经在顶部消失了),并且更新了重用的单元格,这应该显示出不同的东西。

该怎么办:

不要在后台关闭中存储对UICellView的引用,但存储索引路径 在主线程中更新时,检查图像路径是否仍然可见,然后获取属于该路径的单元并更新它。

Apple还有一个很好的教程(在几年前的WWDC上也有演示): https : //developer.apple.com/library/content/samplecode/LazyTableImages/Introduction/Intro.html

The reason is that you are loading the data in a background queue, which takes a while, and once the data is available, you update the cell (in the main thread, which is correct). But while the data has been loaded, you are scrolling the table view up and down, so the cell disappears maybe at the top and will be reused at the bottom for other data. Now the background loading has finisehd (with the image data for the old cell that has already disappeared at the top) and you update the reused cell, which should dispaly something different.

What to do:

Dont store a reference to the UICellView in the background closure, but store the index path When updating in the main thread, check if the image path is still visible, then fetch the cell belonging to that path, and update it.

There is also a nice tutorial by Apple (also demonstrated on some WWDC years ago): https://developer.apple.com/library/content/samplecode/LazyTableImages/Introduction/Intro.html

更多推荐

本文发布于:2023-04-12 19:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/dc3076adf09626146a189cceb3547cad.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   我在   原型   发生了   使用了

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!