gpt4 book ai didi

ios - 一个 View Controller 中具有不同重用单元格的两个 Collection View

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:52 25 4
gpt4 key购买 nike

在我的 View Controller 中,我想要两个 Collection View 。尝试此操作后,我收到 Sigabrt 错误并且我的应用程序崩溃了。我预测问题是因为我正在将这些 Collection View 的数据源分配给自己。我可能是错的,这是我的代码:

在 View 加载时,我设置了 Collection View 的数据源:

@IBOutlet var hashtagCollectionView: UICollectionView!
@IBOutlet var createCollectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
createCollectionView.dataSource = self
hashtagCollectionView.dataSource = self
}

然后我为 UICollectionViewDataSource 创建一个扩展

extension CategoryViewController: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var returnValue = 0

if collectionView == hashtagCollectionView {
// Return number of hashtags
returnValue = hashtags.count
}

if collectionView == createCollectionView {
// I only want 3 cells in the create collection view
returnValue = 3
}
return returnValue
}

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

// Place content into hashtag cells
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "hashtagCell", for: indexPath) as! TrendingTagsCollectionViewCell
cell.hashtagText = hashtags[indexPath.row]

// Place content in creators cell
let createCell = collectionView.dequeueReusableCell(withReuseIdentifier: "createCell", for: indexPath) as! CreateCollectionViewCell
createCell.text = creators[indexPath.row]
createCell.image = creatorImages[indexPath.row]

// Is this the right logic?
if collectionView == hashtagCollectionView {
return_cell = cell
} else {
return_cell = createCell
}
return return_cell
}

}

最佳答案

这会崩溃,因为您在 collectionView(_:cellForItemAt:) 中的 if 语句没有覆盖足够的范围。

虽然您需要根据 Collection View 的要求返回不同的单元格是对的,但您不能使用类似的不同标识符调用 dequeueReusableCell(withReuseIdentifier:for:) 两次。由于您两次询问同一个 Collection View ,很可能其中一个标识符未在该 Collection View 中注册。

相反,您应该扩展 if 以涵盖该方法的几乎全部:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView == hashtagCollectionView {
// Place content into hashtag cells
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "hashtagCell", for: indexPath) as! TrendingTagsCollectionViewCell
cell.hashtagText = hashtags[indexPath.row]
return cell
} else if collectionView == createCollectionView {
// Place content in creators cell
let createCell = collectionView.dequeueReusableCell(withReuseIdentifier: "createCell", for: indexPath) as! CreateCollectionViewCell
createCell.text = creators[indexPath.row]
createCell.image = creatorImages[indexPath.row]

return cell
} else {
preconditionFailure("Unknown collection view!")
}
}

这只会尝试使一个单元格出队一次,具体取决于请求的 Collection View ,并将返回的单元格转换为正确的类。

注意这种方法暂时有效,但从长远来看,您可以获得非常长的 UICollectionViewDataSource 方法实现,所有这些都包含在一系列 if 语句中。可能值得考虑将您的数据源分成单独的较小类。

关于ios - 一个 View Controller 中具有不同重用单元格的两个 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47425422/

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