2017-02-13 209 views
3

我试图使用自定义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 
} 

}

回答

6

你需要调用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")

+0

我想我必须

reusableview.frame = CGRect(0 , 0, self.view.frame.width, headerHight) 
在上面

将xib的注册更改为class,这里现在出现错误:无法将类型视图出列:具有标识符HCollectionReusableView的UICollectionElementKindSectionHeader - 必须注册一个nib或类标识符或连接故事板中的原型单元格 (null) –

+1

您将出现错误,因为标识符在您将该项目取消并且注册时不匹配时尝试在注册该单元格时对此标识符进行硬编码: '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

+0

只有在您完全不使用Xib或Storyboard时,您才会注册课程 – John

0

你设置文件在您XIB文件”所有者设置?将文件所有者更改为托管collectionView的View控制器。

enter image description here

+0

是的,我做到了,在文件所有者和收集可重复使用的视图 –