Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift UICollectionViewCell在创建新单元格之前使用以前的单元格数据填充';s数据加载,需要实现PrepareForReuse()_Swift_Xcode_Uicollectionview_Uicollectionviewcell_Prepareforreuse - Fatal编程技术网

Swift UICollectionViewCell在创建新单元格之前使用以前的单元格数据填充';s数据加载,需要实现PrepareForReuse()

Swift UICollectionViewCell在创建新单元格之前使用以前的单元格数据填充';s数据加载,需要实现PrepareForReuse(),swift,xcode,uicollectionview,uicollectionviewcell,prepareforreuse,Swift,Xcode,Uicollectionview,Uicollectionviewcell,Prepareforreuse,我的UICollectionView有一个问题,问题是如果在显示新数据之前,重复使用之前单元格中的数据滚动足够快(甚至没有那么快)一两秒钟 这是它发生的视频,用于更多上下文(youtube视频链接): 这是cellForItemAt中的代码: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if let

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

这是它发生的视频,用于更多上下文(youtube视频链接):

这是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
}
我非常确定我需要实现的是
prepareforuse()
,但我不确定具体如何实现,也无法在网上找到任何有用的东西


非常感谢

您需要像这样在
feedCollectionViewCell
类中添加
prepareforeuse

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

您需要像这样在
feedCollectionViewCell
类中添加
prepareforeuse

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