@@ -21,7 +21,7 @@ class CollectionViewController: UICollectionViewController {
21
21
let layout = UICollectionViewCompositionalLayout . list ( using: config)
22
22
self . collectionView. collectionViewLayout = layout
23
23
self . collectionView. register ( UICollectionViewListCell . self, forCellWithReuseIdentifier: self . cellId)
24
- self . collectionView. register ( UICollectionReusableView . self, forSupplementaryViewOfKind: UICollectionView . elementKindSectionHeader, withReuseIdentifier: self . headerId)
24
+ self . collectionView. register ( UICollectionViewListCell . self, forSupplementaryViewOfKind: UICollectionView . elementKindSectionHeader, withReuseIdentifier: self . headerId)
25
25
}
26
26
27
27
let data = [ " Manny " , " Moe " , " Jack " ]
@@ -50,21 +50,13 @@ class CollectionViewController: UICollectionViewController {
50
50
}
51
51
}
52
52
53
+ // Apple says: for the header of a collection view list, use a list cell!
53
54
override func collectionView( _ collectionView: UICollectionView , viewForSupplementaryElementOfKind kind: String , at indexPath: IndexPath ) -> UICollectionReusableView {
54
- let v = collectionView. dequeueReusableSupplementaryView ( ofKind: kind, withReuseIdentifier: self . headerId, for: indexPath)
55
- var lab : UIView ! = v. viewWithTag ( 1 )
56
- if lab == nil {
57
- lab = UILabel ( )
58
- lab. tag = 1
59
- lab. translatesAutoresizingMaskIntoConstraints = false
60
- v. addSubview ( lab)
61
- lab. topAnchor. constraint ( equalTo: v. topAnchor, constant: 5 ) . isActive = true
62
- lab. bottomAnchor. constraint ( equalTo: v. bottomAnchor, constant: - 5 ) . isActive = true
63
- lab. leadingAnchor. constraint ( equalTo: v. leadingAnchor, constant: 15 ) . isActive = true
64
- v. backgroundColor = . lightGray
65
- v. layer. zPosition = 1
66
- }
67
- ( lab as! UILabel ) . text = " Pep \( indexPath. section) "
55
+ let v = collectionView. dequeueReusableSupplementaryView ( ofKind: kind, withReuseIdentifier: self . headerId, for: indexPath) as! UICollectionViewListCell
56
+ var contentConfig = v. defaultContentConfiguration ( )
57
+ contentConfig. text = " Pep \( indexPath. section) "
58
+ v. contentConfiguration = contentConfig
59
+ v. layer. zPosition = 1
68
60
return v
69
61
}
70
62
}
0 commit comments