关闭屏幕时更新 UICollectionView 补充视图 header

Update CollectionView supplementaryView header when off screen

我的观点类似于 Instagram 的主要故事提要。我在顶部有一个水平 collection 视图,其中有一个 supplementaryView header 单元格,它看起来相同但与其余单元格的行为不同。

我正在尝试更新 supplementaryView 中的 objects,我已设法使用以下代码完成...

@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)
    }
}

当单元格在视图中时,这工作正常,但是当单元格在调用期间从屏幕上滑出时,它不会更新。

关于如何解决这个问题的任何想法?或者更好的方法来更新 collection 视图中的第一个单元格而不更新其余单元格。

谢谢

header 在您需要时可见时不为零

var color = UIColor.red

里面 viewForSupplementaryElementOfKind

func collectionView(_ collectionView: UICollectionView, 
     viewForSupplementaryElementOfKind kind: String, 
                      at indexPath: IndexPath) -> UICollectionReusableView 
   let  myCell = //
   myCell.nameLbl.textColor = color
   return myCell
}

然后在header再次出现后更改color您想要的位置和时间,它将更改为最新的颜色

@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)
    }
}