[英]How to placed collectionView in respective tableView Section
I have two collectionViews CollectionViewCell
and CollectionViewCellButton
that i want to place in first and second section
of tableView respectively.我有两个 collectionViews
CollectionViewCell
和CollectionViewCellButton
,我想分别放在 tableView 的第一和第二section
。 I am debugging it by setting break point .我正在通过设置断点来调试它。
Here is my tableView.swift file cellForRowAt indexPath
这是我的 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()
}
Here is updateCellWith
method that is called in each section
of tableView这是在 tableView 的每个
section
中调用的updateCellWith
方法
func updateCellWith(row: [CollectionViewModel]) {
self.rowWithColors = row
self.collectionView.reloadData()
}
Here is TableViewCell.Swift file that has cellForItemAt indexPath
for collectionView这是 TableViewCell.Swift 文件,
cellForItemAt indexPath
用于 collectionView 的cellForItemAt indexPath
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()
}
On setting breakPoint in cell.updateCellWith(row: rowArray)
in indexPath.section == 1
of cellForRowAt indexPath
of tableView.The debugger does not enter into indexPath.section == 1
of collectionView function cellForItemAt indexPath
it revisit indexPath.section == 0
of collectionView Just for both section 0 and 1
of tableView cellForRowAt indexPath
在 tableView 的
cellForRowAt indexPath
的indexPath.section == 1
的cell.updateCellWith(row: rowArray)
中indexPath.section == 1
cellForRowAt indexPath
,调试器不会进入 collectionView 函数cellForItemAt indexPath
indexPath.section == 1
它重新访问indexPath.section == 0
collectionView 仅用于 tableView cellForRowAt indexPath
section 0 and 1
It enter into indexPath.section == 0
of collectionView function cellForItemAt indexPath
and give run time error.How i can make it run section 1
of collectionView function on section 1
of tableView.How display both CollectionView on View ?Just like in this figure它进入
indexPath.section == 0
的CollectionView功能cellForItemAt indexPath
上真运行时间error.How我可以使它运行section 1
对的CollectionView功能的section 1
上查看?只是tableView.How同时显示的CollectionView像在这个图
You can download the code from this google drive link run it for batter visualisation and see what is required您可以从此谷歌驱动器链接下载代码运行它以进行电池可视化并查看需要什么
There are multiple issues in your code, answering or fixing all of them is really not in the scope of the question asked, Ill only focus on the crash:您的代码中有多个问题,回答或修复所有问题实际上不在所问问题的范围内,我只关注崩溃:
issue with your code is forced type casting.您的代码问题是强制类型转换。 Error is fairly clear
错误相当明显
Clearly you are holding array of CollectionViewModel
(which is a protocol) and there are two different classes that confirms to this protocol CollectionViewCellModel
and CollectionViewCellModelButton
显然,您持有
CollectionViewModel
数组(这是一个协议),并且有两个不同的类确认该协议CollectionViewCellModel
和CollectionViewCellModelButton
and you blindly force cast both type to CollectionViewCellModel
using你一味的力投两类型
CollectionViewCellModel
使用
(self.rowWithColors?[indexPath.item] as! CollectionViewCellModel).imageView
Hence the crash.因此崩溃。
Whats the issue?这是什么问题?
There are way too many, I'll point out few relevant in this context.有太多的方式,我会指出在这种情况下很少有相关的。 Your
collectionView
inside TableViewCell
always receives an array of either CollectionViewCellModel
or CollectionViewCellModelButton
(One dimensional array)您在
TableViewCell
内的collectionView
总是接收一个CollectionViewCellModel
或CollectionViewCellModelButton
数组(一维数组)
In your code you hard code number of sections as 1在您的代码中,您将节数硬编码为 1
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
But strangely in your cellForItemAt indexPath
you start checking indexPath.section == 0
or indexPath.section == 1
, how can indexPath.section
ever be 1 if your number of sections is hardcoded as 1?但奇怪的是,在您的
cellForItemAt indexPath
您开始检查indexPath.section == 0
或indexPath.section == 1
,如果您的部分数量被硬编码为 1 , indexPath.section
怎么会是 1 ?
Much more strangely you try to type cast the element in array rowWithColors
based on indexPath.section
In your code you typecast (self.rowWithColors?[indexPath.item] as! CollectionViewCellModel)
if indexPath.section == 0
and self.rowWithColors?[indexPath.item] as! CollectionViewCellModelButton
更奇怪的是,您尝试根据
indexPath.section
在数组rowWithColors
键入元素在您的代码中(self.rowWithColors?[indexPath.item] as! CollectionViewCellModel)
if indexPath.section == 0
和self.rowWithColors?[indexPath.item] as! CollectionViewCellModelButton
self.rowWithColors?[indexPath.item] as! CollectionViewCellModelButton
if indexPath.section == 1
self.rowWithColors?[indexPath.item] as! CollectionViewCellModelButton
如果indexPath.section == 1
What you need?你需要什么?
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()
}
O/P:开/关:
TIP:提示:
Not sure why you have cellForItemAtIndexPath
with incorrect signature again in the same file, its good that its not getting called, but it let me wondering why?不知道为什么你的
cellForItemAtIndexPath
在同一个文件中再次出现签名不正确的情况,它没有被调用很好,但它让我想知道为什么? My advice, delete it :)我的建议,删除它:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.