将自定义标题添加到集合视图swift
我试图使用自定义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
}
}
你需要调用viewForSupplementaryElementOfKind
这样的:
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch kind {
case UICollectionElementKindSectionHeader:
let reusableview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView", for: indexPath) as! HCollectionReusableView
reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight)
//do other header related calls or settups
return reusableview
default: fatalError("Unexpected element kind")
}
}
这样就可以初始化并通过这样的扩展UICollectionViewDelegateFlowLayout
显示标题
设置UICollectionViewHeader框架的另一种方式是:
extension UIViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 100) //add your height here
}
}
这消除了需要调用:提到func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
Remember to register the HeaderView after you initialise your UICollectionView by calling:
collectionView.register(UINib(nibName: HCollectionReusableView.nibName, bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HCollectionReusableView")
我想我必须
在上面将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