2015-07-03 70 views
2

我正在處理醫療帳單應用程序,並且我有兩個單元格用於兩種不同類型的醫療代碼。第一個是訪問代碼,第二個是診斷代碼。可以有許多診斷代碼添加到特定的訪問代碼中,並且我試圖創建一個由單個訪問代碼和任意數量的診斷代碼(包括零)組成的部分。UICollectionView與部分中的兩個自定義單元格

var icdCodes:[[(icd10:String,icd9:String)]] = [[]] //A list of diagnoses codes for the bill 
var visitCodes:[String] = [] //A list of the visit codes that have been added 

目前我有一個UICollectionView,我添加訪問代碼。我在爲每個visitCode單元顯示所有icd10單元時遇到問題。我可以將一個「ICD10Cell」出隊,但我不確定indexPath中的單元是visitCodeCell還是ICD10Cell。我的數據源代碼如下:

有誰知道我該如何實現我所尋找的功能?

回答

4

對於任何可能有類似需求的人,我通過將訪問代碼設置爲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") 
相關問題