I added different text labels to my collectionview headers for each section in swift.I use uireusableviews for headers. My problem is when I scroll the text in the labels of different sections overlap with each other.I tried setting the text label to "" before changing the text but the problem still persists.
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!
}