首页 > 解决方案 > 如何将 collectionView 放置在相应的 tableView 部分中

问题描述

我有两个 collectionView CollectionViewCellCollectionViewCellButton我想分别放在sectiontableView 的第一个和第二个。我通过设置断点来调试它。

这是我的 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 == 1collectionView函数cellForItemAt indexPath它重新访问 indexPath.section == 0collectionView 仅适用于section 0 and 1tableViewcellForRowAt indexPath

它进入indexPath.section == 0collectionView函数cellForItemAt indexPath并给出运行时错误。我怎样才能让它section 1在tableView上运行collectionView函数。如何section 1在View上同时显示CollectionView?就像在这个图中

在此处输入图像描述

您可以从此google drive 链接下载代码运行它以进行击球手可视化并查看需要什么

标签: iosswiftxcodeuitableviewuicollectionview

解决方案


您的代码中有多个问题,回答或修复所有问题确实不在所问问题的范围内,我只关注崩溃:

您的代码的问题是强制类型转换。错误相当明显

在此处输入图像描述

显然,您持有的数组CollectionViewModel(这是一个协议)并且有两个不同的类可以确认该协议CollectionViewCellModelCollectionViewCellModelButton

并且您盲目地强制将两种类型都转换为CollectionViewCellModelusing

(self.rowWithColors?[indexPath.item] as! CollectionViewCellModel).imageView

因此崩溃。

什么问题?

太多了,我将在这方面指出一些相关的内容。你的collectionView内部TableViewCell总是收到一个数组CollectionViewCellModel或者CollectionViewCellModelButton(一维数组)

在您的代码中,您将节数硬编码为 1

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

但奇怪的是,您cellForItemAt indexPath开始检查indexPath.section == 0or ,如果您的部分数量硬编码为 1 indexPath.section == 1,怎么可能indexPath.section是 1?

更奇怪的是,您尝试rowWithColors根据indexPath.section 在您的代码中类型转换(self.rowWithColors?[indexPath.item] as! CollectionViewCellModel)ifindexPath.section == 0self.rowWithColors?[indexPath.item] as! CollectionViewCellModelButtonif类型转换数组中的元素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在同一个文件中再次有不正确的签名,它没有被调用很好,但它让我想知道为什么?我的建议,删除它:)


推荐阅读