Swift UICollectionViewCell在创建新单元格之前使用以前的单元格数据填充';s数据加载,需要实现PrepareForReuse()
我的UICollectionView有一个问题,问题是如果在显示新数据之前,重复使用之前单元格中的数据滚动足够快(甚至没有那么快)一两秒钟 这是它发生的视频,用于更多上下文(youtube视频链接): 这是cellForItemAt中的代码: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
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
}
}