多个 tableView 单元格中的多个 collectionView [英] multiple collectionView in multiple tableView cells
问题描述
我有一个 tableView
,其中有 2 个自定义单元格,在两个单元格中我都有不同的 CollectionView
,CollectionViews
的数据源和委托code> 是我的 ViewController
.
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
.
那么现在我如何检查在 UICollectionView's
的各个方法中要配置哪个 CollectionViews
?
So now how do I check which CollectionViews
is to be configured in UICollectionView's
respective methods?
这是视图层次结构
我怎么知道上面函数的参数中的collectionView是哪个?
How do I come to know which is the collectionView that's there in the parameter of above function?
这是我的代码:
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 中的 tag 属性来标识您的集合视图.因此,当您在 cellForRowAtIndexPath 中配置表格单元格时,获取单元格的集合视图并将其标记设置为唯一的(我将使用 indexpath 的行值).
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
}
}
这篇关于多个 tableView 单元格中的多个 collectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!