UICollectionView在一个部分中有两个自定义单元格

时间:2015-07-03 17:15:15

标签: swift uicollectionview uicollectionviewcell

我正在开发医疗账单应用程序,我有两个单元格,用于两种不同类型的医疗代码。第一个是访问代码,第二个是诊断代码。可能有许多诊断代码被添加到特定的访问代码中,我试图使一个部分由单个访问代码和任意数量的诊断代码(包括零)组成。

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。我的dataSource代码如下:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return icdCodes[section].count + 1 //add 1 for the initial visitcode cell
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("visitCodeCell", forIndexPath: indexPath) as! CodeTokenCollectionViewCell

    cell.visitCodeLabel.text = visitCodes[indexPath.row]

    cell.deleteCodeButton.tag = indexPath.row
    return cell
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

    return visitCodes.count
}

有谁知道我如何才能实现我想要的功能?

1 个答案:

答案 0 :(得分: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")