我的 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()
,但我不确定如何做到这一点,并且在网上找不到任何有用的东西。
非常感谢!