我試圖使用自定義xib文件將標題添加到collectionView
。我創建了xib
文件,並執行UICollectionReusableView
的類。 在collectionViewController
我註冊了xib
文件是這樣的:將自定義標題添加到集合視圖swift
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
}
}
我想我必須
在上面將xib的註冊更改爲class,這裏現在出現錯誤:無法將類型視圖出列:具有標識符HCollectionReusableView的UICollectionElementKindSectionHeader - 必須註冊一個nib或類標識符或連接故事板中的原型單元格 (null) –
您將出現錯誤,因爲標識符在您將該項目取消並且註冊時不匹配時嘗試在註冊該單元格時對此標識符進行硬編碼: 'self.collectionView.register(UINib(nibName:HCollectionReusableView.nibName,bundle:nil),forSupplementaryViewOfKind:UICollectionElementKindSectionHeader,withReuseIdentifier:「HCollectionReusableView」) 'This should work ... then you just just adjust the code to fit your needs – John
只有在您完全不使用Xib或Storyboard時,您纔會註冊課程 – John