swift - 关闭屏幕时更新 UICollectionView 补充 View 标题

标签 swift uicollectionview header instagram supplementary

我的观点与 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/

相关文章:

html - 修复 ngx-datatable 中的 header

bash - 从具有从给定位置到特殊字符的模式位置的文件中提取 bash 中的子字符串

swift - CollectionView 单元格和进度栏 - 滚动后进度栏显示在错误的单元格中

ios - iOS 上的地理围栏 - Swift

ios - 按部分水平分页 CollectionView

ios - 无法让我的标题在 swift 4 中显示在我的 UIcollectionView 页面上

swift - Swift 代码错误?

ios - 对成员 'downloadTask(with:completionHandler:)' 的模糊引用

ios - 如何使 UIWebViews 缩放以适合 collectionView 单元格?

opengl - 将基本 3D 模型导入 OpenGL 应用程序