Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPad - More than one UITableView in the same screen

I coding app from iPad and I have to put two separate UITableView in the same screen. For this app I can´t put the UITableView and divid in two sections for requisits reason. It must be two separated. Well, in this case how I can fill the rows of UITableView. Can I have create a DataSource and Delegate in separate classes, one for a first UITableView and other DataSource and Delegate class for the second UITableView or have other approach more elegant?

tks a lot.

like image 392
rwvaldivia Avatar asked Oct 04 '10 19:10

rwvaldivia


2 Answers

You can do this a few different ways. The most straightforward is to use separate classes to handle the datasource and delegate protocols for each table view.

Alternatively, you could use a single class as the datasource and delegate for both, and check the identity of the tableview that's passed into the protocol methods.

It would looks something like this: (I'm assuming this code is on your view controller.)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 44.0; // default height
    if (tableView == self.myLeftTableView) {
        height = // Compute the cell height for the left table view.
    } else {
        height = // Compute the cell height for the right table view.
    }
    return height;
}

This could get ugly quickly, which is why I'd recommend the first approach.

like image 63
Kris Markel Avatar answered Sep 29 '22 11:09

Kris Markel


Yes, you can make different classes for datasource and delegate methods for different UITableView and in-fact this is the best approach for using multiple tables on same view as this approach implements MVC architecture. For this try these 2 solutions in which first approach is for implementing 2 tables datasource and delegate method in same class and second is to implement different datasource and delegate method in different classes either by using UITableViewController or NSObject class

For more detail try these links where you can find sample code too:

Handle more than one table in a View Part-1

Handle more than one table in a View Part-2

like image 26
Soniya Avatar answered Sep 29 '22 12:09

Soniya