Ios 查找链接到“我的表”视图的集合视图单元格

Ios 查找链接到“我的表”视图的集合视图单元格,ios,swift,uitableview,uicollectionview,tableview,Ios,Swift,Uitableview,Uicollectionview,Tableview,我正在寻找您对我的tableview和collection view之间的关联的帮助 我有一个具有6个水平滚动单元格的CollectionView。 在每个单元格中,我想放置一个表视图 对于每个tableView,我需要知道我要将想要的数据放在哪个单元格中 事实上,我的代码用于在单元格内显示tableView,但我被阻止查找我所在的集合视图单元格 顺便说一句,我的餐室控制器实际上是空的,它只有一个简单的插座。没有别的了 这是我的密码 class TrainFoodPlanViewControll

我正在寻找您对我的tableview和collection view之间的关联的帮助

我有一个具有6个水平滚动单元格的CollectionView。 在每个单元格中,我想放置一个表视图

对于每个tableView,我需要知道我要将想要的数据放在哪个单元格中

事实上,我的代码用于在单元格内显示tableView,但我被阻止查找我所在的集合视图单元格

顺便说一句,我的餐室控制器实际上是空的,它只有一个简单的插座。没有别的了

这是我的密码

class TrainFoodPlanViewController: BaseViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate {

    let train_meals_list: [String] = ["breakfast", "snack_1", "pre_intra_post", "lunch", "snack_2", "diner"]

    @IBOutlet weak var trainCollectionView: UICollectionView!


    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "Food Plan"
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return train_meals_list.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! Meal_vc_cell

        cell.meal_number.text = train_meals_list[indexPath.row]

        return cell
    }
}
这就是我要做的

实现这一点的方法有很多。我的建议是将UITableView子类化,并添加parentCollectionViewIndexPath属性

我假设Dine_vc_cell有一个到相关tableView的出口。将该tableView的类型更改为MyCustomTableView

注意:如果您使用的是storyboard,也不要忘记在storyboard中更新tableView的类

现在,只需在collectionView的cellForItemAt方法中设置parentCollectionViewIndexPath


集合视图单元格应为tableview数据源;将适当的数组分配给集合视图的属性可以为单元格的contentView提供一个与其在collectionViewcell.contentView.tag=indexPath.row中的位置相关联的标记,并将该标记提供给celltableView.tag=cell.contentView.tag中的tableView。然后在tableView的委托中,使用该标记来了解它与Switch tableView.tag{…}关联的collectionViewCell。
class MyCustomTableView: UITableView {
    var parentCollectionViewIndexPath: IndexPath?
}
class Meal_vc_cell: UITableViewCell {
    @IBOutlet weak var tableView: MyCustomTableView
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath) as! Meal_vc_cell
    cell.tableView.parentCollectionViewIndexPath = indexPath
    return cell
}