【发布时间】:2020-07-08 17:38:08
【问题描述】:
我在 swift 中为每个部分的 collectionview 标题添加了不同的文本标签。我使用 uireusableviews 作为标题。我的问题是当我滚动不同部分标签中的文本相互重叠时。我尝试在更改文本之前将文本标签设置为“”,但问题仍然存在。
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if(kind == UICollectionView.elementKindSectionHeader){
let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "h", for: indexPath) as? UICollectionViewCell
let txtlab = UILabel()
txtlab.text = ""
if(indexPath.section%2 == 0){
txtlab.text = "Header"}
else{
txtlab.text = "another header"
}
cell?.addSubview(txtlab)
txtlab.translatesAutoresizingMaskIntoConstraints = false
txtlab.centerYAnchor.constraint(equalTo: cell!.centerYAnchor).isActive = true
txtlab.centerXAnchor.constraint(equalTo: cell!.centerXAnchor).isActive = true
cell?.backgroundColor = .green
return cell!
}
【问题讨论】:
标签: swift uicollectionview uicollectionviewlayout uicollectionreusableview