2

I have a view similar to Instagram's main stories feed. I have a horizontal collection view at the top where I have a supplementaryView header cell that looks the same but acts differently to the rest of the cells.

I am trying to update the objects in the supplementaryView which I have managed to do with the following code...

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

This works fine when the cell is in view, however when the cell is swiped off screen during this call it doesn't get updated.

Any ideas on how to get around this? Or a better approach to updating just the first cell in a collection view and not the rest.

Thanks

4

1 回答 1

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)
    }
}
于 2018-09-28T22:37:07.770 回答