0

I am working on a project and I have the need to create two UITableViews on a single storyboard. What is the best way to handle this? I am not sure I like the viewcontroller being the delegate and datasource for both of the uitableviews.

Thornuko
  • 287
  • 1
  • 4
  • This question is overly broad, doesn't show any work you've done to find the answer, and has also been answered many times, including here: http://stackoverflow.com/questions/1416372/multiple-uitableviews-on-one-uiview – Aaron Brager Aug 14 '13 at 20:21

3 Answers3

2

Well, you have a couple of options. The first and most obvious is that you use the two table view's with your single view controller as their delegate/datasource. If you go this route, there really isn't a lot of overhead. Mostly just a if statement here and there and possibly a different cell identifier in cellForRowAtIndexPath:.

However, if you're more interested in organization, you could create a subclass of UITableView and set its delegate/datasource to itself. That way, each instance is handling its delegation/ data supply internally and it doesn't even need to communicate with the view controller that contains it unless you specifically need/tell it to.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
1

This is not hard at all. Create your view controller, add your two table views to it and hook them up to IBOutlets (or create them programmatically and assign them to properties/ivars). Your UITableViewDataSource and UITableViewDelegate methods will look something like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.firstTableView) {
        return [self.firstDataSource sectionCount];
    }
    else if (tableView == self.secondTableView) {
        return [self.secondDataSource sectionCount];
    }
    NSAssert(NO, @"unknown tableView!");
    return 0;
}

Just make sure your delegate and data source methods check which tableView is asking for data (or notifying you of an action/change) and make sure your code handles either case appropriately.

Nicholas Hart
  • 1,734
  • 13
  • 22
0

You can place as many UITableViews on a superview as you need. The containing view controller needn't be the datasource or delegate for either, but it can be for both.

danh
  • 62,181
  • 10
  • 95
  • 136