Strange Strange - 22 days ago 6
Swift Question

multiple collectionView in multiple tableView cells

I have a

tableView
in which I have 2 custom cells, in both cells I have different
CollectionView
, the dataSource and delegate of both the
CollectionViews
is my
ViewController
.

So now how do I check which
CollectionViews
is to be configured in
UICollectionView's
respective methods?

Here is the view hierarchy enter image description here

How do I come to know which is the collectionView that's there in the parameter of above function?

So here is my code:

class FeatureBannerTableViewCell: UITableViewCell {
@IBOutlet weak var featureCollectionView: UICollectionView!
}

class FeaturedTableViewCell: UITableViewCell {
@IBOutlet weak var FeatureTitle: UILabel!
@IBOutlet weak var seeAllButton: UIButton!
@IBOutlet weak var collectionView: UICollectionView!
}

extension ViewController: UITableViewDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch indexPath.row {
case 2,3,6,7:
let cell = featuredTableView.dequeueReusableCellWithIdentifier("FeatureBannerTableViewCell", forIndexPath: indexPath) as! FeatureBannerTableViewCell
cell.featureCollectionView.dataSource = self
cell.featureCollectionView.delegate = self
return cell
default:
let cell = featuredTableView.dequeueReusableCellWithIdentifier("FeaturedTableViewCell", forIndexPath: indexPath) as! FeaturedTableViewCell
cell.collectionView.dataSource = self
cell.collectionView.delegate = self
return cell
}
}
}

extension ViewController: UICollectionViewDataSource {
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// how do I know if this collectionView is collectionView or featureCollectionView?

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// how do I know if this collectionView is collectionView or featureCollectionView?

}
}

Answer

I would use the tag property in UIView to identify your collection view. So when you configure your table cells in cellForRowAtIndexPath get the collection view of the cell and set its tag to something unique (I would use the row value of indexpath).

extension ViewController: UITableViewDataSource {
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        switch indexPath.row {
        case 2,3,6,7:
            let cell = featuredTableView.dequeueReusableCellWithIdentifier("FeatureBannerTableViewCell", forIndexPath: indexPath) as! FeatureBannerTableViewCell
            cell.featureCollectionView.dataSource = self
            cell.featureCollectionView.delegate = self

            cell.featureCollectionView.tag = indexPath.row

            return cell
        default:
            let cell = featuredTableView.dequeueReusableCellWithIdentifier("FeaturedTableViewCell", forIndexPath: indexPath) as! FeaturedTableViewCell
            cell.collectionView.dataSource = self
            cell.collectionView.delegate = self

            cell.collectionView.tag = indexPath.row

            return cell
        }
    }
}

Then in your collection view methods get the tag value and that will tell you which one of the many collection views it is.

extension ViewController: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // how do I know if this collectionView is collectionView or featureCollectionView?
        let datasourceIndex = collectionView.tag
        // now use your datasource for the following index

    }

}