ios - 如何将 collectionView 放置在相应的 tableView 部分中
问题描述
我有两个 collectionView CollectionViewCell
,CollectionViewCellButton
我想分别放在section
tableView 的第一个和第二个。我通过设置断点来调试它。
这是我的 tableView.swift 文件cellForRowAt indexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0{
if let cell = tableView.dequeueReusableCell(withIdentifier: "tableviewcellid", for: indexPath) as? TableViewCell {
// Show SubCategory Title
(colorsArray.objectsArray[indexPath.section] as! TableViewCellModel).headerButton.setTitle("View All",for: .normal)
cell.category.text = (colorsArray.objectsArray[indexPath.section] as! TableViewCellModel).category
cell.headerButton.setTitle("View All", for: .normal)
// Pass the data to colletionview inside the tableviewcell
// debugging...
let v1 = colorsArray.objectsArray[indexPath.section]
// what is v1?
print(type(of: v1))
guard let thisTableCellModel = v1 as? TableViewCellModel else {
fatalError("Expected a TableViewCellModel !!!")
}
let v3 = thisTableCellModel.colors[indexPath.row]
// what is v3?
print("123Array=",type(of: v3)) // this is an Array, not a CollectionViewCellModel
guard let rowArray = v3 as? [CollectionViewCellModel] else {
fatalError("Expected a [CollectionViewCellModel] !!!")
}
// if we get here, we have properly unwrapped
// an array of CollectionViewCellModel
// so, don't make it an Array of Array
//cell.updateCellWith(row: [rowArray])
print("rowArray1=",type(of: rowArray))
cell.updateCellWith(row: rowArray)
cell.selectionStyle = .none
return cell
}
}
else if indexPath.section == 1{
if let cell = tableView.dequeueReusableCell(withIdentifier: "tableviewcellid", for: indexPath) as? TableViewCell {
(colorsArray.objectsArray[indexPath.section] as! TableViewCellModel).headerButton.setTitle("View All",for: .normal)
cell.category.text = (colorsArray.objectsArray[indexPath.section] as! TableViewCellModel).category
cell.headerButton.setTitle("View All", for: .normal)
// if let cell = ((colorsArray.objectsArray[indexPath.section] as! TableViewCellModel).colors as! CollectionViewCellModelButton)
// debugging...
let v1 = colorsArray.objectsArray[indexPath.section]
// what is v1?
print(type(of: v1))
guard let thisTableCellModel = v1 as? TableViewCellModel else {
fatalError("Expected a TableViewCellModel !!!")
}
let v3 = thisTableCellModel.colors[indexPath.row]
// what is v3?
print("123ArraynotNotmodel=",type(of: v3)) // this is an Array, not a CollectionViewCellModel
guard let rowArray = v3 as? [CollectionViewCellModelButton] else {
fatalError("Expected a [CollectionViewCellModelButton] !!!")
}
print("rowArray2=",type(of: rowArray))
cell.updateCellWith(row: rowArray) //break point
}
}
return UITableViewCell()
}
这是在每个tableViewupdateCellWith
中调用的方法section
func updateCellWith(row: [CollectionViewModel]) {
self.rowWithColors = row
self.collectionView.reloadData()
}
这是cellForItemAt indexPath
用于 collectionView的 TableViewCell.Swift 文件
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0
{
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionviewcellid", for: indexPath) as? CollectionViewCell {
// cell.imageView = UIImage(named: (rowWithColors?[indexPath.item].image)! )
// CollectionViewCellModel
/// [CollectionViewCellModelButton
print("index1=",indexPath.section)
cell.imageView.image = (self.rowWithColors?[indexPath.item] as! CollectionViewCellModel).imageView
cell.dicountAmountLabel.text = (self.rowWithColors?[indexPath.item] as! CollectionViewCellModel).dicountAmountLabel
cell.dicountLabel.text = (self.rowWithColors?[indexPath.item] as! CollectionViewCellModel).dicountLabel
cell.customerTypeLabel.text = (self.rowWithColors?[indexPath.item] as! CollectionViewCellModel).customerTypeLabel
cell.dicountAmountLabel.textColor = .white
cell.dicountLabel.textColor = .white
cell.customerTypeLabel.textColor = .white
return cell
}
}
else if indexPath.section == 1
{
print("index2=",indexPath.section)
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionviewcellButtonid", for: indexPath) as? CollectionViewCellButton {
// cell.imageView = UIImage(named: (rowWithColors2?[indexPath.item].image)! )
//if let model = self.rowWithColors?[indexPath.item] as? CollectionViewCellModelButton {
//model.collectionButton.setTitle(model.collectionButton, for: .normal)
//cell.collectionButton.titleLabel?.text = (self.rowWithColors?[indexPath.item] as! CollectionViewCellModelButton).collectionButton.setTitle("Hi", for: .normal)
cell.collectionButton.setTitle("Hi", for: .normal)
//
return cell
}
}
return UICollectionViewCell()
}
cell.updateCellWith(row: rowArray)
在tableViewindexPath.section == 1
中设置断点。cellForRowAt indexPath
调试器没有进入indexPath.section == 1
collectionView函数cellForItemAt indexPath
它重新访问 indexPath.section == 0
collectionView 仅适用于section 0 and 1
tableViewcellForRowAt indexPath
它进入indexPath.section == 0
collectionView函数cellForItemAt indexPath
并给出运行时错误。我怎样才能让它section 1
在tableView上运行collectionView函数。如何section 1
在View上同时显示CollectionView?就像在这个图中
您可以从此google drive 链接下载代码运行它以进行击球手可视化并查看需要什么
解决方案
您的代码中有多个问题,回答或修复所有问题确实不在所问问题的范围内,我只关注崩溃:
您的代码的问题是强制类型转换。错误相当明显
显然,您持有的数组CollectionViewModel
(这是一个协议)并且有两个不同的类可以确认该协议CollectionViewCellModel
和CollectionViewCellModelButton
并且您盲目地强制将两种类型都转换为CollectionViewCellModel
using
(self.rowWithColors?[indexPath.item] as! CollectionViewCellModel).imageView
因此崩溃。
什么问题?
太多了,我将在这方面指出一些相关的内容。你的collectionView
内部TableViewCell
总是收到一个数组CollectionViewCellModel
或者CollectionViewCellModelButton
(一维数组)
在您的代码中,您将节数硬编码为 1
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
但奇怪的是,您cellForItemAt indexPath
开始检查indexPath.section == 0
or ,如果您的部分数量硬编码为 1 indexPath.section == 1
,怎么可能indexPath.section
是 1?
更奇怪的是,您尝试rowWithColors
根据indexPath.section
在您的代码中类型转换(self.rowWithColors?[indexPath.item] as! CollectionViewCellModel)
ifindexPath.section == 0
和self.rowWithColors?[indexPath.item] as! CollectionViewCellModelButton
if类型转换数组中的元素indexPath.section == 1
你需要什么?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let collectionViewCellModel = self.rowWithColors?[indexPath.row] as? CollectionViewCellModel,
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionviewcellid", for: indexPath) as? CollectionViewCell {
cell.imageView.image = collectionViewCellModel.imageView
cell.dicountAmountLabel.text = collectionViewCellModel.dicountAmountLabel
cell.dicountLabel.text = collectionViewCellModel.dicountLabel
cell.customerTypeLabel.text = collectionViewCellModel.customerTypeLabel
cell.dicountAmountLabel.textColor = .white
cell.dicountLabel.textColor = .white
cell.customerTypeLabel.textColor = .white
return cell
}
if let collectionViewCellModelButton = self.rowWithColors?[indexPath.row] as? CollectionViewCellModelButton,
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionviewcellButtonid", for: indexPath) as? CollectionViewCellButton
{
//customise `CollectionViewCellButton` here with `collectionViewCellModelButton` data
return cell
}
return UICollectionViewCell()
}
输出/输出:
提示:
不知道为什么你cellForItemAtIndexPath
在同一个文件中再次有不正确的签名,它没有被调用很好,但它让我想知道为什么?我的建议,删除它:)
推荐阅读
- wxpython - SetFont 不断失败,参数不匹配 - Apple OS
- operator-overloading - Raku 运算符重载
- tensorflow - 无法使 TensorFlow 与 pypy3 和 conda 一起工作。无效的 ELF
- javascript - Javascript新日期输出错误时间
- python - Python Selenium Web Table Scraping问题
- java - Spring boot:将消息推送到每个请求的特定主题
- arrays - Perl:显示 HoA 中的元素差异
- javascript - jQuery 过滤器 - 如何设置不同的活动按钮,而不是“全部”?
- c - 将 FreeRTOS 库添加到 Energia IDE
- php - 如何使用 PHP 重新排序 HTML 下拉列表?