滚动时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
发布评论