AMR AL-KHAYAT
AMR AL-KHAYAT

Reputation: 75

How to add multi arrays in one section of Collection View Cell which that collection view inside tableView cell?

-Hello guys I have UIViewContoller consist of TableView and collectionView. The tableView has three Sections and third one contains dynamic cell .The third cell contains collectionView also is dynamic.

-The problem is with CollectionView ,

I have 3 arrays each array contains names of images

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

I should show those arrays in one collectionViewCell , I don't want add them in one array because each array belongs to different row in the tableview, when I run app and open this page and I am trying to select the last index of the collectionView my app crash and tell me indexPath is out of range.

//TableView code

      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()
        }

// collectionView Code

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)
    }

3- this func to generate the current array

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
}

Upvotes: 0

Views: 1549

Answers (1)

Related Questions