【问题标题】:How to placed collectionView in respective tableView Section如何将 collectionView 放置在相应的 tableView 部分中
【发布时间】:2021-12-06 02:04:18
【问题描述】:

我有两个collectionViews CollectionViewCellCollectionViewCellButton,我想分别放在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 == 1cellForRowAt indexPathindexPath.section == 1cell.updateCellWith(row: rowArray)设置断点。调试器没有进入collectionView函数cellForItemAt indexPathindexPath.section == 1它重新访问collectionView的indexPath.section == 0,只针对section 0 and 1section 0 and 1表视图cellForRowAt indexPath

它进入collectionView 函数cellForItemAt indexPathindexPath.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


【解决方案1】:

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

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

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

你盲目地强制将这两种类型都转换为CollectionViewCellModel 使用

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

因此发生了崩溃。

有什么问题?

有太多太多了,我将指出一些与此上下文相关的内容。你在TableViewCell 中的collectionView 总是接收CollectionViewCellModelCollectionViewCellModelButton 的数组(一维数组)

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

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

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

更奇怪的是,您尝试根据indexPath.section 类型转换数组rowWithColors 中的元素 在您的代码中,如果indexPath.section == 0self.rowWithColors?[indexPath.item] as! CollectionViewCellModelButton 如果indexPath.section == 1 则输入(self.rowWithColors?[indexPath.item] as! CollectionViewCellModel)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()
    }

O/P:

提示:

不知道为什么cellForItemAtIndexPath 在同一个文件中再次出现签名不正确,它没有被调用很好,但它让我想知道为什么?我的建议,删除它:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多