Aman Chawla
Aman Chawla

Reputation: 229

Populating CollectionView which is inside TableView from API

I'm using alamofire to get data from an API stores inside an array with the help of a model class. Now i want to display this data in a CollectionView that is inside a TableView. So passing that array to display the data is being a problem here is some code see if you can find the problem.

Tell me if you want me to give you anymore information

cellforrowat of tableview

if let cell = tableView.dequeueReusableCell(withIdentifier: "HeadlineRow", for: indexPath) as? HeadlineRow {

            let latest = self.latest[indexPath.section]
            cell.updateUI(latest: latest)

            return cell
        }

tableviewcell class

var latestNews = [LatestNews]()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeadlineCell", for: indexPath) as? HeadlineCell {

            let latest = self.latestNews[indexPath.row]
            cell.updateUI(latest: latest)

            return cell
        }

        return UICollectionViewCell()
    }

    func updateUI(latest: LatestNews) {
        self.latestNews.append(latest)
    }

collection view class

func updateUI(latest: LatestNews){

        self.headlineImg.sd_setImage(with: URL(string: latest.thumbnail))
        headlineTextView.text = latest.headline
    }

So The problem basically is how do i transfer the array from MainViewController to the custom TableView class to the custom Collection View Class.

Upvotes: 0

Views: 1298

Answers (1)

nuridin selimko
nuridin selimko

Reputation: 342

UITableViewCell class:

class MyTableViewCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout
{
 var data : [SomeData]?

@IBOutlet var collectionView: UICollectionView! // initialize your collectionview from storyboard

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return data.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "myCellIdentifier", for: indexPath) as! MyCustomCollectionViewCell
    return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 100, height: 100)
}
func initCollection()
{
    self.collectionView.dataSource = self
    self.collectionView.delegate = self
    self.collectionView.register(UINib(nibName:"MyCustomCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "myCellIdentifier")
}


func setData(data: [SomeData])
{
    self.data = data
    self.initCollection()
    self.collectionView.reloadData()
}
}

cellForRowAt of tableview:

let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCellIdentifier", for: indexPath) as! MyTableViewCell
cell.setData(data: yourArray)

you can also initialize "data" array from cellForRowAt like:

cell.data = yourArray

this is a sample that I've done for my project that contains horizontal collectionview in tableviewcell

Upvotes: 4

Related Questions