【发布时间】:2021-12-06 02:04:18
【问题描述】:
我有两个collectionViews CollectionViewCell 和CollectionViewCellButton,我想分别放在tableView 的第一个和第二个section 中。我是通过设置断点来调试的。
这是我的 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()
}
这是在tableView的每个section中调用的updateCellWith方法
func updateCellWith(row: [CollectionViewModel]) {
self.rowWithColors = row
self.collectionView.reloadData()
}
这是 TableViewCell.Swift 文件,其中包含 cellForItemAt indexPath 用于 collectionView
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()
}
在tableView的indexPath.section == 1的cellForRowAt indexPath的indexPath.section == 1的cell.updateCellWith(row: rowArray)设置断点。调试器没有进入collectionView函数cellForItemAt indexPath的indexPath.section == 1它重新访问collectionView的indexPath.section == 0,只针对section 0 and 1的section 0 and 1表视图cellForRowAt indexPath
它进入collectionView 函数cellForItemAt indexPath 的indexPath.section == 0 并给出运行时错误。我怎样才能让它在tableView 的section 1 上运行collectionView 函数的section 1。如何在View 上同时显示CollectionView?就像在这个图
您可以从google drive link 下载代码,运行它以进行击球手可视化,看看需要什么
【问题讨论】:
-
2天以来你一直在添加相同的问题,链接stackoverflow.com/questions/69605067/…stackoverflow.com/questions/69616026/…请不要添加重复的问题,如果您没有得到答案,请细化您的问题添加相关数据为了帮助我们调试,编辑您的问题以使其简洁
-
我已经删除了他们,并要求新的
-
检查我添加的答案,如果您觉得有用,请考虑通过单击我的答案旁边的勾号来接受答案
标签: ios swift xcode uitableview uicollectionview