jignesh kasundra
jignesh kasundra

Reputation: 101

How to pass multidimensional array in collection view inside the tableview cell?

I have multidimensional array for example like following, how can I pass the array of my collection view on / of a table views / viewed cel ?

let array = [["abc " , " bce "] , ["a "] , ["abc " , " abce " , " a "] , []]

I want first array object of first cell, second array object of second cell and continuous.

Upvotes: 3

Views: 1409

Answers (1)

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

You can implement like this way

extension YourViewController : UICollectionViewDelegate,UICollectionViewDataSource {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return array.count
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return array[section].count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath)
        let string = array[indexPath.section][indexPath.item]
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let selectedString = array[indexPath.section][indexPath.item]
    }

}

Upvotes: 2

Related Questions