多个tableView中的多个collectionView

我有一个tableView我有2个自定义单元格,在这两个单元格中我有不同的CollectionView ,这两个CollectionView的数据源和委托是我的ViewController

那么现在我该如何检查在UICollectionView's各自的方法中configuration了哪些CollectionViews

这是视图层次结构 在这里输入图像说明

我如何才能知道上面函数的参数中有哪些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? } } 

我会使用UIView中的标签属性来识别你的集合视图。 所以当你在cellForRowAtIndexPath中configuration你的表格单元格的时候,获取单元格的集合视图,并将它的标签设置为唯一的东西(我将使用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 } } 

我想,你可以使用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 }