多个tableView单元格中的多个collectionView

时间:2016-11-16 12:35:10

标签: ios swift uitableview uicollectionview

我有一个tableView我有两个自定义单元格,在这两个单元格中我有不同的CollectionViewCollectionViews的dataSource和委托就是我的ViewController

那么现在如何检查CollectionViews各个方法中要配置哪个UICollectionView's

以下是视图层次结构enter image description here

我如何知道上面函数参数中哪个是collectionView?

所以这是我的代码:

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?

    }
}

2 个答案:

答案 0 :(得分:3)

我会使用UIView中的tag属性来标识您的集合视图。因此,当您在cellForRowAtIndexPath中配置表单元格时,获取单元格的集合视图并将其标记设置为唯一的(我将使用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
        }
    }
}

然后在你的集合视图中,方法获取标记值,并告诉你它是多少集合视图中的哪一个。

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

    }

}

答案 1 :(得分:1)

我认为,您可以使用isKindOfClass

示例:

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
}