如何解决在加载新的单元格数据之前,需要用以前的单元格数据填充UICollectionViewCell,需要实现PrepareForReuse
我的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()
,但是我不确定该怎么做,也找不到在线有用的东西。
非常感谢!
解决方法
您需要像这样在prepareForReuse
类中添加feedCollectionViewCell
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
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。