2015-07-03 48 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") 
相关问题