首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法创建UICollectionReusableView的IBOutlet

无法创建UICollectionReusableView的IBOutlet
EN

Stack Overflow用户
提问于 2019-01-03 17:58:57
回答 1查看 329关注 0票数 2

我正面临着一个陌生的问题。我在collectionView中设置了集合视图头的UICollectionReusableView。它完全可以工作,但是当我在UICollectionReusableView中创建UILabelIBOutlet时,它总是在集合视图头方法viewForSupplementaryElementOfKind中返回nil。在XIB文件中,UILabel已成功连接。

这是我的代码。注册CollectionView标头

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-03 19:16:01

解决了!!

我使用XIB作为集合视图头,所以我的注册方式是不使用collection view XIB。我已经改变了它

collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")

代码语言:javascript
复制
collectionView.register(UINib(nibName: "Header", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54019954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档