UICollectionView 在一个部分中有两个自定义单元格
UICollectionView with two custom cells in a section
我正在开发一个医疗账单应用程序,我有两个单元格用于两种不同类型的医疗代码。第一个是访问代码,第二个是诊断代码。可以有许多诊断代码被添加到一个特定的访问代码,我试图让一个部分包含一个单一的访问代码和任意数量的诊断代码(包括零)。
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。我的数据源代码如下:
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
}
有谁知道我怎样才能实现我正在寻找的那种功能?
对于可能有类似需求的任何人,我通过将访问代码设为 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() 方法中指定 header 大小。自定义单元格 类 也需要在 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")
我正在开发一个医疗账单应用程序,我有两个单元格用于两种不同类型的医疗代码。第一个是访问代码,第二个是诊断代码。可以有许多诊断代码被添加到一个特定的访问代码,我试图让一个部分包含一个单一的访问代码和任意数量的诊断代码(包括零)。
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。我的数据源代码如下:
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
}
有谁知道我怎样才能实现我正在寻找的那种功能?
对于可能有类似需求的任何人,我通过将访问代码设为 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() 方法中指定 header 大小。自定义单元格 类 也需要在 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")