TableView inside specific UICollectionViewCell programmatically?

10,930

Solution 1

For those who need it, i found the solution:

class CustomizedCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate {

    var tableView = UITableView()
    let cellIdentifier: String = "tableCell"

    override func layoutSubviews() {
        super.layoutSubviews()

        tableView.delegate = self
        tableView.dataSource = self

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
    }

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

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 4
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: cellIdentifier)

        cell.textLabel?.text = "1 CUP"
        cell.detailTextLabel?.text = "Whole"

        return cell
    }
}

Then at viewDidLoad method at CollectionView i did this:

collectionView?.register(CustomizedCell.self, forCellWithReuseIdentifier: "cell")

And after that i have called like this at cellForRowAt indexPath method of UICollectionView:

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomizedCell

    return cell
}

Solution 2

You can create a custom UICollectionView cell for that indexpath. And add a tableview in the cell xib.

Implement the delegate methods of tableview in custom cell class.

 class CustomCollectionViewCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate
  {
    @IBOutlet var tableView: UITableView!

    override func layoutSubviews() 
    {
        super.layoutSubviews()
        tableView.delegate = self
        tableView.dataSource = self
    }
  }
Share:
10,930

Related videos on Youtube

Labinot Bajrami
Author by

Labinot Bajrami

Updated on June 04, 2022

Comments

  • Labinot Bajrami
    Labinot Bajrami almost 2 years

    i am trying to add a TableView inside UICollectionViewCell, so i want to manage that cell by myself from TableView. So to be more clear, if indexPath.row is 2, then i want to call my tableView cell's inside collectionview indexPath.row.

    Please check the picture, i have made with red colour what i want to do. I created everything programmatically, using UICollectionViewController and UICollectionViewControllerFlowLayout.

  • skymook
    skymook over 6 years
    I don't see when you add the UITableview to the collection cell?
  • Tigran Iskandaryan
    Tigran Iskandaryan about 6 years
    Didn't this violate MVC pattern? Cells shouldn't know anything about data because they are just views
  • Ruiz
    Ruiz almost 6 years
    @TigranIskandaryan Only if you're using MVC, I guess. :^)
  • user3390652
    user3390652 over 5 years
    Is this implementation able to inherit the index of the CollectionViewCell itself? Or do I need to obtain it via the CollectionViewDelegate protocol ??
  • user3390652
    user3390652 over 5 years
    So how do you assign the data to the tableview in different collection cells??
  • Alok
    Alok over 3 years
    What if tableview cell has dynamic height
  • Vaibhav Jhaveri
    Vaibhav Jhaveri almost 3 years
    @b-m-gevariya How did you among the height of the cells, both CollectionView and TableView cell?