0

I have a main view controller containing a table view, and that table view contains a cell which contains another table view. I want the main view controller to act as the delegate and data source of both table views, except how can I differentiate between the two if one is in a table view cell class. I tried differentiating based on tags and names (which was in other SO answers) but they didn't work since the table views aren't all located in the same view controller class. Thanks in advance.

Eric
  • 199
  • 1
  • 11
  • Hey Eri, first thing why do you want delegates and datasource of tableview inside the cell, you can add delegate and datasource inside cell class and also pass your mainviewcontroller data obj there, also if you want this so that you can access some properties then on that case create protocols and handle them – Shivam Gaur Oct 17 '18 at 10:18
  • refer -> https://stackoverflow.com/questions/16195660/multiple-uitableview-in-single-viewcontroller – SPatel Oct 17 '18 at 10:29

2 Answers2

0

I think you can check with the name of the table view in rowAtindexPath and number of cellin section method. Like

if tableView == tableViewOne { return 10 } else if tableView == tableViewTwo { return 20 }

0

Since you mentioned that you have a UITableView inside another UITableView's cell, I assume that the inner tableView's outlet is owned by the outer tableView's cell. Hence, I believe that you won't be able to make the direct equivalency check.

There may be different approaches to solve this.

One quick approach would be to create 2 UITableView sub-classes and use one each for each type of tableView (For example, OuterTableView and InnerTableView classes; or creating even 1 sub-class would serve the purpose).

Sample code:

class OuterTableView: UITableView {
}

class InnerTableView: UITableView {
}

class YourViewController: UIViewController, UITableViewDataSource {

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

        if tableView is OuterTableView {
            // Return the corresponding row count.
            return 2
        } else if tableView is InnerTableView {
            // Return the corresponding row count.
            return 1
        } else {
            return 0
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //
        //  You may use only one sub-class also for the identification
        //
        if tableView is OuterTableView {
            return UITableViewCell()
        } else {
            return UITableViewCell()
        }
    }
}

Note: Don't forget to assign the corresponding classes to your UITableView elements in the Interface builder.

D V
  • 348
  • 2
  • 10
  • Your answer was just what I was looking for! Quick question: When I use your technique and subclass the table view it requires a frame. Does that frame matter since I just anchor the table view anyways to the boundaries of the cell? So far I've just set random height and width values for the frame and everything's worked as expected, but I'm just not sure if setting random values will affect anything down the line. – Eric Oct 21 '18 at 02:38
  • Cheers! No, you don't have to worry about anything else. You can treat these in all different ways you treat and configure the normal tableViews. It will behave normally. – D V Oct 21 '18 at 04:47