首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一个节中有两个自定义单元格的UICollectionView

一个节中有两个自定义单元格的UICollectionView
EN

Stack Overflow用户
提问于 2015-07-03 17:15:15
回答 1查看 2.9K关注 0票数 2

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

代码语言:javascript
复制
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,我添加了访问代码。我在显示每个icd10单元的所有visitCode单元时遇到了问题。我可以排掉" ICD10Cell“的队列,但我不确定indexPath的单元格是visitCodeCell还是ICD10Cell。我的dataSource代码如下:

代码语言:javascript
复制
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
}

有人知道我怎样才能实现我想要的那种功能吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-06 20:09:37

对于可能有类似需求的人,我通过将访问代码作为头单元格并使用基于数据源的部分来解决我的问题。CollectionView方法如下:

代码语言:javascript
复制
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()方法中注册。

代码语言:javascript
复制
    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")
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31211663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档