【问题标题】:UICollectionViewCell is populated with previous cells data before new the cell's data loads, need to implement PrepareForReuse()UICollectionViewCell 在新单元格的数据加载之前填充了以前的单元格数据,需要实现 PrepareForReuse()
【发布时间】:2020-12-01 12:05:52
【问题描述】:

我的 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(),但我不知道具体该怎么做,并且在网上找不到任何有用的东西。

非常感谢!

【问题讨论】:

    标签: swift xcode uicollectionview uicollectionviewcell prepareforreuse


    【解决方案1】:

    你需要像这样在feedCollectionViewCell类中添加prepareForReuse

    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多