ios - CollectionView 标题中的标签问题

标签 ios swift uicollectionview uilabel

我试图将标签插入到标题中,但是当我滚动 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/

相关文章:

ios - MagicalRecord 2.3 临时对象

ios - pod 安装 : CocoaPods could not find compatible versions for pod "Firebase/CoreOnly" 上的 Flutter 错误

ios - 如何在 iOS 中创建自动完成 [实时] 文本字段,它会在您键入时提供建议

ios - swift 仅满足某些要求时如何进入另一个segue?

ios - swift:解压缩文件时显示 UIActivityIndi​​catorView

ios - 如何构建图形多维数组

ios - 访问数组中对象的属性 - Swift 3.0

ios - 在另一个动画之前在 alpha 上平滑地暂停重复的 UIView block 动画

ios - 如何在 `UICollectionViewCell` 位置更改时正确移动数组项

ios - 如果 Collection View 嵌入在 ios 中的 TableView 中,如何始终显示 Collection View 的第一项