我试图将标签插入到标题中,但是当我滚动 collectionView 时,另一个标签被添加到 View 中。我怎么能阻止这个? enter image description here
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath)
header.backgroundColor = .white
let label = UILabel(frame: header.bounds)
label.text = colorsArray[indexPath.section]
header.addSubview(label)
return header
}
最佳答案
您需要对 header 进行子类化并在其中添加标签。所以你不会每次都在标题中添加标签。
删除所有标签并重新添加是非常糟糕的做法,而且开销很大。
关于ios - CollectionView 标题中的标签问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44119148/