對於任何可能有類似需求的人,我通過將訪問代碼設置爲Header單元格並使用基於我的數據源的部分來解決了我的問題。該方法的CollectionView低於:
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return visitCodes.count
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return icdCodes[section].count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CONTENT", forIndexPath: indexPath) as! ICD10Cell
let sectionCodes:[(icd10:String, icd9:String)] = icdCodes[indexPath.section]
let (icd10String, icd9String) = sectionCodes[indexPath.row]
cell.ICDLabel.text = icd10String
return cell
}
func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let cell = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "HEADER", forIndexPath: indexPath) as! CodeTokenCollectionViewCell
cell.visitCodeLabel.text = visitCodes[indexPath.section]
cell.deleteCodeButton.tag = indexPath.section
return cell
}
abort()
}
如果不使用IB佈局需要指定,你需要在viewDidLoad中()方法來指定頭大小。自定義單元類也需要在viewDidLoad()方法中註冊。
let layout = codeCollectionView.collectionViewLayout
let flow = layout as! UICollectionViewFlowLayout
flow.headerReferenceSize = CGSizeMake(100, 25)
codeCollectionView.registerClass(ICD10Cell.self, forCellWithReuseIdentifier: "CONTENT")
codeCollectionView.registerClass(CodeTokenCollectionViewCell.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HEADER")