1

我的 UICollectionView 有问题,问题是,如果您滚动得足够快(甚至不是那么快),那么在重用之前单元格中的数据会在显示新数据之前显示一两秒钟。

这是有关更多上下文的视频(youtube 视频链接):https ://youtu.be/I63hBuxBGI0

这是 cellForItemAt 内部的代码:

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

if let post = feedElements[indexPath.row] as? FeedPost {

  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! feedCollectionViewCell

 
  cell.delegate = self
  cell.post = post
  cell.commentButton.tag =  indexPath.row

  // assigning all of the data 
  cell.captionLabel.text = post.caption
  cell.locationLabel.text = post.location
  cell.timeAgoLabel.text = post.timeAgoString
  cell.setUpElements()
  cell.prepareForReuse()
  return cell
}
}

numberOfRowsInSection 内部的代码:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return feedElements.count
}

我很确定我需要实施的是prepareForReuse(),但我不确定如何做到这一点,并且在网上找不到任何有用的东西。

非常感谢!

4

1 回答 1

0

你需要像这样添加prepareForReusefeedCollectionViewCell

class feedCollectionViewCell: UITableViewCell {
    override func prepareForReuse() {
        super.prepareForReuse()

  // Reset your values here as you want ... 

        self.post = nil
        self.commentButton.tag =  0

        // assigning all of the data
        self.captionLabel.text = nil
        self.locationLabel.text = nil
        self.timeAgoLabel.text = nil
    }
}
于 2020-08-11T19:04:59.830 回答