multiple collectionView in multiple tableView cell

2019-05-05 09:41发布

问题:

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

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?

    }
}

回答1:

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

    }

}


回答2:

I think, you can make use of isKindOfClass

Example:

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

 // get a reference to our storyboard cell
 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyReuseIdentifier", for: indexPath as IndexPath) as! UICollectionViewCell

    if cell.isKindOfClass(FeaturedBannerCollectionCell){
         //implementation code
    } else cell.isKindOfClass(FeaturedCollectionViewCell) {
        //implementation code
    }

   return cell
}