我的观点与 Instagram 的主要故事提要类似。我在顶部有一个水平 Collection View ,其中有一个supplementaryView标题单元格,它看起来相同,但与其他单元格的行为不同。
我正在尝试更新supplementaryView中的对象,我已经使用以下代码成功完成了...
@objc func uploadCompleted() {
DispatchQueue.main.async {
self.collectionView.performBatchUpdates({
if let myCell = self.collectionView.supplementaryView(forElementKind: UICollectionElementKindSectionHeader, at: IndexPath(item: 0, section: 0)) as? MyCollectionReusableView {
// Do your stuff here
myCell.nameLbl.textColor = .black
...
}
}, completion: nil)
}
}
当单元格处于 View 中时,这工作正常,但是当单元格在调用期间从屏幕上滑出时,它不会更新。
关于如何解决这个问题有什么想法吗?或者是一种更好的方法,仅更新 Collection View 中的第一个单元格,而不更新其余单元格。
谢谢
最佳答案
当您需要时,标题不为零
var color = UIColor.red
并在viewForSupplementaryElementOfKind
内部
func collectionView(_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView
let myCell = //
myCell.nameLbl.textColor = color
return myCell
}
然后在标题再次出现后,在您想要的位置和时间更改颜色
,它将更改为最新的颜色
@objc func uploadCompleted() {
DispatchQueue.main.async {
self.color = UIColor.black
self.collectionView.performBatchUpdates({
if let myCell = self.collectionView.supplementaryView(forElementKind: UICollectionElementKindSectionHeader, at: IndexPath(item: 0, section: 0)) as? MyCollectionReusableView {
// Do your stuff here
myCell.nameLbl.textColor = self.color
...
}
}, completion: nil)
}
}
关于swift - 关闭屏幕时更新 UICollectionView 补充 View 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52563402/