【问题标题】:Update CollectionView supplementaryView header when off screen关闭屏幕时更新 UICollectionView 补充视图标题
【发布时间】:2018-09-28 22:33:44
【问题描述】:

我的观点类似于 Instagram 的主要故事提要。我在顶部有一个水平集合视图,其中有一个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)
    }
}

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

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

谢谢

【问题讨论】:

    标签: swift uicollectionview header instagram supplementary


    【解决方案1】:

    当你需要时,标题不为零

    var color = UIColor.red
    

    viewForSupplementaryElementOfKind里面

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

    然后在标题再次出现后更改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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      相关资源
      最近更新 更多