Search code examples
swiftuitableviewscrolluicollectionviewcell

UITableView in every UICollectionView Cell


I've been searching about getting a UITableView inside UICollectionView cells, but so far I have only found the exact oposite: CollectionViews inside TableView cells. My approach is the following:

Collection View Controller:

class CollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

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

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

        return cell

    }
}

Custom CollectionViewCell:

class CollectionViewCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        let tableView = UITableView()
        self.contentView.addSubview(tableView)
    }

}

The goal is to have information displayed in pages that scroll horizontally, and each page can have many rows (thus TableViews inside CollectionViews). However I can't seem to figure out how to have one TableViewController for every TableView instance added to the CollectionView's cells.

Thanks in advance.


Solution

  • Not sure if this really deserves an answer.. but circling back to our comment discussion...

    You can conform UICollectionView to the tableview protocols just like any UIView or UIViewController.