【发布时间】: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