richardtulkan
richardtulkan

Reputation: 41

Is it possible to have a UITableView under a UICollectionView?

Long story short. I have a ViewController that has a ColletionView that's the main component of the page. The CollectionView has 2 reusable cell a Header and a Footer. I would like to have a TableView inside the footer but that's not possible I get the following error when I try to connect the TableView:

"Illegal Configuration: The tableView outlet from the ViewController to the UITableView is invalid. Outlets cannot be connected to repeating content."

My tip is that I want to achieve this in a total wrong way.

I want to achieve the following:

enter image description here

Upvotes: 2

Views: 366

Answers (2)

karem_gohar
karem_gohar

Reputation: 336

I assume you did register the footer/header nib like this:

segmentCollectionView.register(UINib(nibName: /* CellNibName */, bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: /* CellId */)

and then dequeue the cell like this

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionView.elementKindSectionFooter {
        let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: /* CellId */, for: indexPath) as! /* Your Class */
    } 
    else {
    }
}
  • so the trick is in your registered cell class as a footer you can go to the xib file and add the UITableView inside the reference /* Your Class */
  • link the UITableView in the cell class not theUIViewControllerclass that contains theUICollectionView`
  • then pass the data you want when you dequeue the cell and perform all your business inside the cell class

Upvotes: 0

richardtulkan
richardtulkan

Reputation: 41

[SOLVED]

The solution was the following: I made a CollectionReusableView file for the Footer then exactly when @karem_gohar said I passed the following:

footer.tableView.delegate = footer.self footer.tableView.dataSource = footer.self

I connected the tableView to the CollectionReusableView, I made an array to test it out I filled the tableView with the elements of the array.

Ps.:I made a TableViewCell and linked a label to it. The label showed the element of the array.

Upvotes: 1

Related Questions