我正面临着一个陌生的问题。我在collectionView中设置了集合视图头的UICollectionReusableView。它完全可以工作,但是当我在UICollectionReusableView中创建UILabel的IBOutlet时,它总是在集合视图头方法viewForSupplementaryElementOfKind中返回nil。在XIB文件中,UILabel已成功连接。
这是我的代码。注册CollectionView标头
collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) as! Header
headerView.lblName.text = "Hello" //crash here
return headerView
}这里有完整的演示https://drive.google.com/open?id=1v2lWsQstfUiWDEldVH3Ru2HeB3LGWiXZ
发布于 2019-01-03 19:16:01
解决了!!
我使用XIB作为集合视图头,所以我的注册方式是不使用collection view XIB。我已经改变了它
collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")
至
collectionView.register(UINib(nibName: "Header", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")https://stackoverflow.com/questions/54019954
复制相似问题