gpt4 book ai didi

ios - 为什么两个 TableView 单元格中的两个 Collection View 在 Swift 4 中不起作用?

转载 作者:可可西里 更新时间:2023-11-01 01:59:54 25 4
gpt4 key购买 nike

我阅读了类似的问题,例如如何在多个 TableView 单元格中拥有多个 Collection View ,并且我连接了我的 Collection View 单元格并为它们使用了标识符名称,但我不知道为什么会收到此错误:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier extera_infoCollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' * First throw call stack:

** 请记住,我读过类似的问题,第一个带有 Collection View 的 TableView 单元格运行良好,问题是第二个 **这是我的主视图 Controller 代码,它有一个 TableView , TableView 有两个单元格

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

if collectionView == fieldOfActivityCell().fieldofActivitiesCollectionView {
let fullfields : String = self.adv.resultValue[0].work_field!
let fullfieldsArr : [String] = fullfields.components(separatedBy: ",")
print(fullfieldsArr)
return fullfieldsArr.count

} else {

let extera_infofields : String = self.adv.resultValue[0].extera_info!
let extera_infofieldsArr : [String] = extera_infofields.components(separatedBy: ",")
print(extera_infofieldsArr)
return extera_infofieldsArr.count
}
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if collectionView == fieldOfActivityCell().fieldofActivitiesCollectionView {

let fieldsCells = collectionView.dequeueReusableCell(withReuseIdentifier: "fieldOfActivityCollectionViewCell", for: indexPath) as! fieldOfActivityCollectionViewCell

let fullfields : String = self.adv.resultValue[0].work_field!
let fullfieldsArr : [String] = fullfields.components(separatedBy: ",")

fieldsCells.title.text = fullfieldsArr[indexPath.row]

return fieldsCells
}
else {
let extera_infoCells = collectionView.dequeueReusableCell(withReuseIdentifier: "extera_infoCollectionViewCell", for: indexPath) as! extera_infoCollectionViewCell

let extera_info : String = self.adv.resultValue[0].extera_info!
let extera_infoArr : [String] = extera_info.components(separatedBy: ",")

extera_infoCells.infoText.text = extera_infoArr[indexPath.row]

return extera_infoCells
}
}

这是同一 View Controller 中的 TableView 代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

if indexPath.row == 0{

let fieldCell = self.showAdvTableView.dequeueReusableCell(withIdentifier: "fieldOfActivityCell", for: indexPath) as! fieldOfActivityCell

return fieldCell

} else {

let fieldCell = self.showAdvTableView.dequeueReusableCell(withIdentifier: "extera_infoCell", for: indexPath) as! extera_infoCell

return fieldCell
}

这是 TableView 第一个单元格类:

class fieldOfActivityCell: UITableViewCell {

@IBOutlet weak var fieldofActivitiesCollectionView: UICollectionView!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code

if let flowLayout = fieldofActivitiesCollectionView.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.estimatedItemSize = CGSize.init(width: 1.0, height: 1.0) }
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}
}

extension fieldOfActivityCell {

func setCollectionViewDataSourceDelegate
<D: UICollectionViewDelegate & UICollectionViewDataSource>
(_ dataSourceDelegate:D , forRow row : Int )

{
fieldofActivitiesCollectionView.delegate = dataSourceDelegate
fieldofActivitiesCollectionView.dataSource = dataSourceDelegate
fieldofActivitiesCollectionView.reloadData()
}
}

这是第二个 tableview 单元类:

@IBOutlet weak var extra_infoCollectionView: UICollectionView!

override func awakeFromNib() {
super.awakeFromNib()

if let flowLayout = extra_infoCollectionView.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.estimatedItemSize = CGSize.init(width: 1.0, height: 1.0) }
}
}

extension extera_infoCell {

func setCollectionViewDataSourceDelegate
<D: UICollectionViewDelegate & UICollectionViewDataSource>
(_ dataSourceDelegate:D , forRow row : Int )

{
extra_infoCollectionView.delegate = dataSourceDelegate
extra_infoCollectionView.dataSource = dataSourceDelegate
extra_infoCollectionView.reloadData()
}
}

enter image description here

最佳答案

第一步:使用标签 - 您只需要为它们使用标签,然后使用 if else 来选择使用标签选择了哪个 Collection View ,所以答案是这样的:

if collectionView.tag == 1 {
do some thing//////
}else {
do some thing else}

你应该在 cellForRowAtIndexPath 和 numberOfRows 方法中使用它你也可以将它用于 TableView

第二步:您必须在 CollectionView 数据源的 cellForRowAt 方法中更改要出列的“ Collection View ”的名称:

if collectionView.tag == 1 {
let cell = yourFirstCollectionView.dequeueReusableCell(...) as yourCell
....
return cell
} else {
let cell = yourSecondCollectionView.dequeueReusableCell(...) as yourCell
....
return cell
}

关于ios - 为什么两个 TableView 单元格中的两个 Collection View 在 Swift 4 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47530933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com