2017-02-13 213 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

是的,我做到了,在文件所有者和收集可重複使用的視圖 –