【发布时间】:2017-02-13 08:21:34
【问题描述】:
我正在尝试使用自定义 xib 文件将标题添加到 collectionView。我用实现UICollectionReusableView 的类创建了xib 文件。
在collectionViewController 我注册了xib 文件,如下所示:
self.collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier)
然后在viewForSupplementaryElementOfKind 我做到了
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: HCollectionReusableView.reuseIdentifier, for: indexPath) as! HCollectionReusableView
尺寸调整
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 100, height: 50)
}
我收到错误消息:无法在捆绑包中加载 NIB。 任何缺少的代码?
HCollectionReusableView 类:
class HCollectionReusableView: UICollectionReusableView {
static var nibName : String
{
get { return "headerNIB"}
}
static var reuseIdentifier: String
{
get { return "headerCell"}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
【问题讨论】:
标签: swift uicollectionview uicollectionreusableview