ios - 多个 tableView 单元格中的多个 collectionView

标签 ios swift uitableview uicollectionview

我有一个 tableView其中我有 2 个自定义单元格,在两个单元格中我都有不同的 CollectionView , CollectionViews 的数据源和委托(delegate)是我的ViewController .

那么现在我该如何检查哪个 CollectionViewsUICollectionView's中配置各自的方法?

这是 View 层次结构 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?

    }
}

最佳答案

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

然后在您的 Collection View 方法中获取标签值,这将告诉您它是众多 Collection View 中的哪一个。

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

    }

}

关于ios - 多个 tableView 单元格中的多个 collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40632326/

相关文章:

ios - 使用 iOS 6 链接到 App Store 应用开发者页面?

ios - 即使队列中有项目,skipPrevious 和 skipNext 按钮也无效 Google Cast iOS Sender SDK v4.3.5 及更高版本

iphone - 如何重新加载当前的 UIView

ios - 核心数据单元未正确更新

ios - 如何将文本字段用作 TableView 的 "search bar"

ios - 使用自动布局时何时将 imageView 添加到 UIButton

ios - 具有不同字体大小的两个自动布局的UILabel:文本底部对齐

ios - 我无法快速将数组写入 NSUserDefaults

开源 swift 项目的 Swift.org 问题。错误 : xcrun: error: unable to find utility "launch-with-toolchain", 不是开发人员工具或在 PATH 中

swift - MVC:在哪里存储数据?