swift - 无法创建 UICollectionReusableView 的 IBOutlet
问题描述
我面临着陌生的问题。我已经获取UICollectionReusableView
了集合视图标题并将其设置在 collectionView 中。它完全可以工作,但是当我创建它IBOutlet
时UILabel
,UICollectionReusableView
它总是在集合视图标题方法中返回 nil viewForSupplementaryElementOfKind
。InXIB
文件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
解决方案
解决了!!
我正在使用XIB
集合视图标题,所以我注册的方式是没有collection view XIB
. 我已经改变了
collectionView.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")
到
collectionView.register(UINib(nibName: "Header", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header")
推荐阅读
- c - 指针和奇怪的 fgets 行为
- fortran - 如何获取可分配字符串的长度?
- azure - 如何在 Azure Devops 中为生产版本创建单独的构建工件
- python - 在python中使用pyodbc获取mssql表列名和数据类型
- datetime - Joda DateTimeFormatter 用于格式为 2020-05-16 01:00+0200 的字符串
- powershell - PowerShell 排除文件夹但删除内容
- laravel - 使用 smtp gmail 在 Laravel 中发送邮件
- css - 如何在primefaces selectManyCheckbox->selectItems->label上应用样式?
- google-api - 通过 isAppAuthorized 属性查询 Google Drive 文件
- python - 有没有以下问题的解决方案