我有兩個collectionViewsCollectionViewCell和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()
}
這是updateCellWith在每個sectiontableView 中呼叫的方法
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)中indexPath.section == 1的cellForRowAt indexPathtableView.The的除錯器不進入indexPath.section == 1的CollectionView功能的cellForItemAt indexPath它重溫 indexPath.section == 0的CollectionView只是兩個section 0 and 1的tableView的cellForRowAt indexPath
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

You can download the code from this 
很明顯,你持有的陣列CollectionViewModel(這是一個協議),并且有兩個不同的類證實了這個協議CollectionViewCellModel,CollectionViewCellModelButton
并且您盲目地強制將兩種型別強制轉換為CollectionViewCellModel使用
(self.rowWithColors?[indexPath.item] as! CollectionViewCellModel).imageView
因此崩潰。
這是什么問題?
有太多的方式,我會指出在這種情況下很少有相關的。你的collectionView里面TableViewCell總是收到一個CollectionViewCellModelor的陣列CollectionViewCellModelButton(一維陣列)
在您的代碼中,您將節數硬編碼為 1
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
但奇怪的是,在cellForItemAt indexPath您開始檢查indexPath.section == 0or 時indexPath.section == 1,indexPath.section如果您的部分數量被硬編碼為 1 ,那么怎么可能是 1?
更奇怪的是你嘗試型別轉換元件陣列rowWithColors根據indexPath.section
在你的代碼的型別轉換(self.rowWithColors?[indexPath.item] as! CollectionViewCellModel),如果indexPath.section == 0和self.rowWithColors?[indexPath.item] as! CollectionViewCellModelButton如果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在同一個檔案中又出現了不正確的簽名,它沒有被呼叫很好,但它讓我想知道為什么?我的建議,洗掉它:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/326248.html
標籤:ios swift xcode uitableview uicollectionview
上一篇:Mp3音頻無法快速播放
