简体   繁体   English

滚动时集合视图可重用单元格问题

[英]Collection View reusable cell issue while scrolling

I am using the collection view to show the gif's on the list.我正在使用集合视图在列表中显示 gif。 Now facing the cell reusable issue while scrolling the cells up or down of collection view.现在在集合视图中向上或向下滚动单元格时面临单元格可重用问题。

Like itemA is on first place in the list and itemB is on the second place in the list.就像itemA在列表中的第一位itemB在列表中的第二位。 but when I scroll the data in the collection view.但是当我在集合视图中滚动数据时。 the places of items got misplaced.物品的位置放错了地方。 like some time itemA gone on 5th place or sometimes anywhere in the list.就像某个时间 itemA 排在第 5 位或有时在列表中的任何位置。 i know i think this is the use with reusable cell, but don't know how to salve this.我知道我认为这是可重复使用的电池的用途,但不知道如何解决这个问题。 Plss help.请帮忙。

Collection view cellForItemAt集合视图 cellForItemAt

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GifCell", for: indexPath as IndexPath) as? GifCell else {
        fatalError()
    }

    if gifArr.count > 0 {

        let urlString = self.gifArr[indexPath.row]
        let url = URL(string: urlString)!

        DispatchQueue.global().async {
            let imageData = try? Data(contentsOf: url)
            let imageData3 = FLAnimatedImage(animatedGIFData: imageData) // this is the 3rd pary library to show the gifs on UIimageview's
            DispatchQueue.main.async {
                cell.imageView.animatedImage = imageData3
                cell.textLabel.text = String(indexPath.row)
            }
        }
    }
    return cell
}

In GifCell you could implement prepareForReuse() method:GifCell您可以实现prepareForReuse()方法:

Performs any clean up necessary to prepare the view for use again.执行任何必要的清理以准备再次使用视图。

override func prepareForReuse() {
    super.prepareForReuse()
    imageView.animatedImage = nil
    textLabel.text = ""
}

Note : at this point, each time cellForItemAt method gets called, the url will be reloaded, so later, you might want find a way to cache the images instead of keep reloading them.注意:此时,每次调用cellForItemAt方法时,都会重新加载 url,因此稍后,您可能需要找到一种方法来缓存图像而不是继续重新加载它们。

First solution : You can cache data and every time check if there is, use your cache.第一个解决方案:您可以缓存数据,每次检查是否有数据时,使用您的缓存。 you can use this link , but replace UIImage with gift type!您可以使用此链接,但将UIImage替换为礼物类型!

or try this, I did not test it或者试试这个,我没有测试过

if let giftAny = UserDefaults.standard.value(forKey: "giftUrl") {
  //cast giftAny to Data
  // use cached gift
} else {
  // cache gift
  let giftData = try? Data(contentsOf: url)
  UserDefaults.standard.setValue(giftData, forKeyPath: "giftUrl")
  //use gift
}

Second Solution : Don't reuse cell第二种解决方案:不要重复使用单元格

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = UICollectionViewCell(style: .default, reuseIdentifier:"Cell")
        return cell
    }

but in this case, if you have many cells, memory leak is unavoidable.但在这种情况下,如果你有很多单元格,内存泄漏是不可避免的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM