简体   繁体   中英

Find which collection view cell is linked to my table view

I'm looking for your help for my association between a tableview and a collection view.

I've got a CollectionView with 6 horizontal scrollable cells. In each cell, I would like to put a table view.

For each tableView, I need to know in which cell I am to put my wanted data.

Actually, I make my code work for displaying tableView inside the cells but I'm blocked for finding in which collection view cell I am.

BTW, my Meal_vc_cell controller is actually Empty, it just has a simple outlet. Nothing else.

Here is my code

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
    }
}

Here is what I'm trying to do在此处输入图片说明

There are a number of ways this can be achieved. My recommendation is to subclass UITableView and add a parentCollectionViewIndexPath property

class MyCustomTableView: UITableView {
    var parentCollectionViewIndexPath: IndexPath?
}

I'm assuming Meal_vc_cell has an outlet to the relevant tableView. Change that tableView's type to MyCustomTableView

class Meal_vc_cell: UITableViewCell {
    @IBOutlet weak var tableView: MyCustomTableView
}

Note: If you're using storyboard, don't forget to update the tableView's class in storyboard as well

Now, just set parentCollectionViewIndexPath in the collectionView's cellForItemAt method

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM