当我快速滚动时,图像在collectionView中正在变化
滚动图像更改时该怎么办?
来自json的数据:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell: CollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
let mindata = (data[indexPath.row] as! NSDictionary)
cell.NameShow!.text = mindata["name"] as? String
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
{
if let url = NSURL(string: (mindata["img"] as? String)!),
data = NSData(contentsOfURL: url)
{
dispatch_async(dispatch_get_main_queue(),
{
cell.img.image = UIImage(data: data)
})
}
})
}
最佳答案
您的代码不完整,所以我可以建议一些提示:
UITableView和UICollectionView都使用重用技术,这意味着一旦创建池,就可以从池中重用单元。
考虑到您要说的问题,您似乎没有清理单元,也没有将图像绑定到特定位置。
您可以根据需要尝试:
子类UICollectionViewCell,并在prepareReuse
中清理图像,以便清理单元格,如果您不在乎顺序,则可以提供所需的任何图像。
将图像与单元格的indexPath
绑定,这意味着您可以创建图像数组或使用某种索引标记图像。
例如在cellForItemAtIndexPath
中,
func collectionView(_ collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
...
cell.img.image = imageArray[indexPath.row]
...
}
关于xcode - 当我快速滚动时,图像在collectionView中发生变化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37062513/