【问题标题】:How to add multi arrays in one section of Collection View Cell which that collection view inside tableView cell?如何在集合视图单元格的一个部分中添加多个数组,该集合视图位于 tableView 单元格内?
【发布时间】:2019-12-02 23:17:30
【问题描述】:

-大家好,我的 UIViewContoller 由 TableView 和 collectionView 组成。 tableView 有三个 Sections,第三个包含动态单元格。第三个单元格包含 collectionView 也是动态的。

-问题出在 CollectionView 上,

我有 3 个数组,每个数组都包含图像的名称

    var dataSheet = ["1", "2", "3"] 
    var partList  = ["4", "5", "6"] 
    var drawing  = ["7", "8", "9"] 

我应该在一个 collectionViewCell 中显示这些数组,我不想将它们添加到一个数组中,因为每个数组属于 tableview 中的不同行,当我运行应用程序并打开此页面并尝试选择最后一个索引时的 collectionView 我的应用程序崩溃并告诉我 indexPath 超出范围。

//表格视图代码

      func numberOfSections(in tableView: UITableView) -> Int {
            return 4
        }

     func tableView(_ tableView: UITableView, numberOfRowsInSection   section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1 :
            return 1
        case 1 :
            return 1
        case 2 :
            return tabelviewSectionNumber.count
        default:
            return 0
        }
    }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
        switch indexPath.section {
        case 0:
            let ComponentkksCell = tableView.dequeueReusableCell(withIdentifier: "componentKKS", for: indexPath) as! componentKKS_TVC

            ComponentkksCell.setUp(data: DescriptionArray[indexPath.row])
            return ComponentkksCell


        case 1 :
            let SystemDescriptionCell = tableView.dequeueReusableCell(withIdentifier: "SystemDescription_TVC", for: indexPath) as! SystemDescription_TVC
           SystemDescriptionCell.setUp(data: DescriptionArray[indexPath.row])
            return SystemDescriptionCell

        case 2:
            let DataSheetCell = tableView.dequeueReusableCell(withIdentifier: "DataSheet_TVC", for: indexPath) as! DataSheet_TVC
            DataSheetCell.setTableViewDelegateAndDataSource(dataSourceDelegate: self, forRow: indexPath.row)
            DataSheetCell.titlesections.text = tabelviewSectionNumber[indexPath.row]
           self.indexPath.append(indexPath.row)

            return DataSheetCell



        default:
            return UITableViewCell()
        }

//集合查看代码

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

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if shareVar.shared.mech == true {
            return getCurrentArray()?.count ?? 0
        } else if shareVar.shared.elec == true {
            return getCurrentArray()?.count ?? 0
        } else {
        return 0
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let dataCell = collectionView.dequeueReusableCell(withReuseIdentifier: "dataImages", for: indexPath) as! SEH_DRA_COMCell
        if let currentImage = getCurrentArray()?[indexPath.row]{
            if shareVar.shared.mech == true {
                dataCell.imageData.image = UIImage(named: currentImage )

            } else if shareVar.shared.elec == true {
                dataCell.imageData.image = UIImage(named: currentImage )
            }
        }

        return dataCell
    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let currentImage = getCurrentArray()?[indexPath.item] else {return}
             print(indexPath.item)
          print(currentImage)
        presenter?.didSelectImage(image: currentImage)
    }
  • 我在一个 collectionView 单元格中添加了 3 个数组 1-我创建了 2 个数组

    var elecData: [Int: [String]] = [:] var mechData : [Int : [String]] = [:] 2-然后我将每个数组添加到我创建的那些数组中

    self.mechData[0] = 数据表 self.mechData[1] = 零件清单 self.mechData [2] = 绘图 self.elecData [0] = 数据表 self.elecData[1] = 接线

3-这个函数生成当前数组

func getCurrentArray() -> [String]? {
    var array = [String]()
    if shareVar.shared.mech == true {
        for index in indexPath {
            array = self.mechData[index] ?? [""]
        }
    } else if shareVar.shared.elec == true{
        for index in indexPath {
        array = self.elecData[index] ?? [""]
        }
    }
    return array
}

【问题讨论】:

    标签: ios arrays swift uicollectionview


    【解决方案1】:

    【讨论】:

    • 我以前读过这篇文章,当你提到再读一遍时,我又读了一遍,我遗漏了一些东西,现在工作得很好。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多