pjmanning
pjmanning

Reputation: 1311

3 Collection Views on One View Controller

I have 3 collection views on 1 view controller. I've tried a few of the other suggestions I've found on Stack but nothing seems to work.

All 3 Collection Views are in a separate cell of a HomeTableViewController. I tried to create the outlet connection to the HomeTableViewController but I get the error Outlets cannot be connected to repeating content.

I've read many people being able to hook up their multiple collectionViews so I am a bit confused as to where I'm going wrong...

Upvotes: 1

Views: 1227

Answers (4)

V D Purohit
V D Purohit

Reputation: 1189

Create three different collection view with different collection view cell, then after you just need to add in dataSource method like below:-

if collectionView == collectionViewA{

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

        return cell
    }else if collectionView == collectionViewB{

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

        return cell
    }else if collectionView == collectionViewC{

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

        return cell
    }else{
        return UICOllectionViewCell()
}

also perform same for other dataSource method.

Upvotes: 0

user7624610
user7624610

Reputation:

You should create outlet in the UITableViewCell. Then you can provide tags to collectionViews in each cell in tableView:cellForRowAtIndexPath Method:

yourCell.collectionViewOutlet.tag = indexPath.row + 1000

you should replace 1000 with Any Constant Integer if the tags conflict with tags of other views.

Then use these tags to differentiate all collectionviews in collectionview:cellForItemForIndexpath method:

if(collectionView.tag == 1000){
//Code for collection view in first row of the table
}
else if(collectionView.tag == 1001){
//Code for collection view in second row of the table
}
else if(collectionView.tag == 1002){
//Code for collection view in third row of the table
}

You should also keep in mind to return number of items in collectionView:numberOfItemsInSection for each collection view just like above.

Tags make the life whole lot easier , don't they? Happy Coding (Y)

Upvotes: 0

user7624610
user7624610

Reputation:

You should create outlet in the UITableViewCell. Then you can provide tags to collectionViews in each cell in tableView:cellForRowAtIndexPath Method:

yourCell.collectionViewOutlet.tag = indexPath.row + 1000

you should replace 1000 with Any Constant Integer if the tags conflict with tags of other views.

Then use these tags to differentiate all collectionviews in collectionview:cellForItemForIndexpath method:

if(collectionView.tag == 1000){
//Code for collection view in first row of the table
}
else if(collectionView.tag == 1001){
//Code for collection view in second row of the table
}
else if(collectionView.tag == 1002){
//Code for collection view in third row of the table
}

You should also keep in mind to return number of items for each collection view just like above.

Tags make the life whole lot easier , don't they? Happy Coding (Y)

Upvotes: 0

Infinity James
Infinity James

Reputation: 4735

The UICollectionView instances cannot be hooked up to IBOutlet properties in a separate UITableViewController.

As you describe, the UICollectionViews are actually each children of their own parent UITableViewCell, and as such are not direct descendants of the UITableViewController. This is because the cells will be added to the UITableView at run time.

If you are set on creating the outlets within the HomeTableViewController I would suggest creating them like so:

private weak var collectionViewA: UICollectionView?

and overriding cellForRow like so:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = super.tableView(tableView, cellForRowAt: indexPath)
    // cast cell as collection view parent
    collectionViewA = cell.collectionView
    return cell
}

As has been mentioned, the better solution would be to manage the UICollectionView instances from within their own UITableViewCell parents. Example:

final class CollectionViewAParentTableViewCell: UITableViewCell {
    @IBOutlet private weak var collectionView: UICollectionView!
}
extension CollectionViewAParentTableViewCell: UICollectionViewDataSource {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        …
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        …
    }
}

Upvotes: 1

Related Questions