Same data is repeating in sections in UITableView and UICollectionView in swift 4

Viewed 354

I am new in swift working on an app, where I am binding data in a collectionView which is inside UITableView, I am sending data (which are Images) in UICollectionView as an multidimensional array like this

[[<UIImage: 0x600000292610>, {1280, 720}, <UIImage: 0x600000292bb0>, {1000, 563}, <UIImage: 0x600000292c50>, {2048, 1200}, <UIImage: 0x6000002926b0>, {1080, 675}, <UIImage: 0x600000292c00>, {1156, 577}, <UIImage: 0x60800028e650>, {322, 156}], [<UIImage: 0x60800028e290>, {1600, 840}, <UIImage: 0x60800028de30>, {1640, 878}, <UIImage: 0x600000294e10>, {322, 156}, <UIImage: 0x600000294d70>, {1917, 1080}, <UIImage: 0x600000295310>, {1311, 737}, <UIImage: 0x60800028dac0>, {258, 195}], [<UIImage: 0x600000295720>, {1601, 1281}, <UIImage: 0x600000295630>, {1600, 1341}, <UIImage: 0x60800028f500>, {1200, 919}, <UIImage: 0x600000296e90>, {4088, 2921}], [<UIImage: 0x608000290ae0>, {1600, 584}, <UIImage: 0x600000297160>, {1400, 1050}, <UIImage: 0x600000296b70>, {717, 476}, <UIImage: 0x608000290e50>, {2280, 1060}, <UIImage: 0x600000297390>, {1068, 427}, <UIImage: 0x608000291260>, {3200, 1600}, <UIImage: 0x608000290e00>, {258, 195}], [<UIImage: 0x608000291490>, {1300, 630}, <UIImage: 0x6080002914e0>, {512, 366}, <UIImage: 0x608000290bd0>, {1200, 628}], [<UIImage: 0x600000297b60>, {1200, 628}, <UIImage: 0x6000002972f0>, {1536, 1024}, <UIImage: 0x608000291b70>, {1540, 705}, <UIImage: 0x6000002985b0>, {1200, 676}, <UIImage: 0x60800009c4d0>, {443, 377}]]

My issue is i am seeing sections 0,1,2 were being created uniquely but then section 3 was being generated the same as section 0,

Here is my code in collectionView

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("numberOfItemsInSection ******",self.imageArray[section].count,"********")
         return self.imageArray[section].count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell: CollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? CollectionViewCell
        {
           // if(imageArray[indexPath.section].count>0){
                print("---> ",indexPath.section," --- item ->",indexPath.row)
                cell.imageView.image = imageArray[indexPath.section][indexPath.row]
            //}
            return cell
        }
        return UICollectionViewCell()
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        print("numberOfSections ******",self.imageArray.count,"********")
        return self.imageArray.count
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        let size = CGSize(width: 120, height: 300)
        return size
    }

This is my tableView Controller

extension CompanyViewController: UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if self.sectionHeaderName.count>0 {
            print("*******",self.sectionHeaderName[section],"*******")
            return self.sectionHeaderName[section]
        }else{
            return ""
        }
    }

    //NUMBER OF SECTION WE WANT IN A TABLE
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.sectionHeaderName.count
    }
    /*NUMNBER OF ROWS IN A SECTION*/
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? TableViewCell
        {

            return cell
        }
        return UITableViewCell()
    }
}

I am struggling here since 2 days but couldn't find any solution. Any Help would be appreciated

TIA

2 Answers

You should set data source or delegate in TableViewCell willDisplay.

func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) {
    collectionView.delegate = dataSourceDelegate
    collectionView.dataSource = dataSourceDelegate
    collectionView.reloadData()
}
override func tableView(tableView: UITableView,
    willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath) {

    guard let tableViewCell = cell as? TableViewCell else { return }

    tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row)
}

I have finally found the answer after scratching head for 3 days

I had to pass my data from TableView Extension in cellForRowAt function like this

cell.cellData = self.imageArray[indexPath.section]
return cell

And in TableViewCell where I defined my CollectionView I have declared the variable

 var cellData = [UIImage]()

and Used the same variable to get the Images in all collection view functions

In CellForItemAt Need to get data from array like below and used row instead of items and section because when scrolling the tableView up and down image may get shuffle or wrongly displayed in cell, so by using indexPath.row it will prevent shuffling of Images.

cell?.imageView.image = cellData[indexPath.row]

One more thing which I need to add is

cell.collectionView.reloadData() 

because TableView was taking reference fo my first array index instead of what I am passing.

Thanks

Related