我有一个 UICollectionView
以水平流布局显示自定义单元格;换句话说,一些内容被放置在屏幕边界之外。
此外,我有一个触发 NSNotification
的手势,导致我的单元格的某些元素(即主题)发生颜色变化。除了出现在屏幕边界之外的单元格不会全部更新为新的颜色变化之外,一切都完美无缺。有没有办法强制他们重绘?
在触发 NSNotification
时调用的函数中,我尝试使用 self.collectionView.reloadData()
、self.collectionView 重绘 Collection View 。 setNeedsDisplay()
和 self.collectionView.setNeedsLayout
但无济于事。我在自定义单元类的 awakeFromNib()
中尝试了列表的最后两个,但没有。
这是我的 cellForItemAtIndexPath
的代码:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = popularCollectionView!.dequeueReusableCellWithReuseIdentifier("popular", forIndexPath: indexPath) as! PopularPainting
cell.thumbnail.image = paintings[indexPath.row].paintingImage
cell.name!.text = paintings[indexPath.row].paintingName
cell.price!.setTitle(paintings[indexPath.row].paintingPrice, forState: UIControlState.Normal)
if cell.isDark {
cell.name!.textColor = UIColor(red: 205/255, green: 205/255, blue: 205/255, alpha: 1)
cell.price!.setTitleColor(self.kPaleBlue, forState: .Normal)
self.popularCollectionView.reloadData()
}
return cell
}
有什么建议吗?
注意:滚动到屏幕外的内容并重复更改主题的手势非常有效,所以我不知道发生了什么。
最佳答案
您假设 Collection View 中的每个项目都存在屏幕外的单元格是不正确的。事实上,表格 View 和 Collection View 会重复使用滚动到屏幕外的单元格,以便新单元格出现在屏幕上,因此存在的单元格只有一屏多一点。
您可以在通知触发后调用 reloadData
。但是您需要确保您的 collectionView:itemForRowAtIndexPath:
实现将正确配置随后在屏幕上滚动的单元格。这可能意味着在通知触发后将状态更改保存在属性中,并在 collectionView:itemForRowAtIndexPath:
中配置单元格时检查该属性。
关于ios - 为什么屏幕外的 UICollectionViewCells 不会更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30706696/