Jenny
Jenny

Reputation: 39

Two UITableViews in one file

Is it possible to have two UITableViews using the same controller file? If so how do you differentiate the two? If not then how would you control them?

Upvotes: 2

Views: 261

Answers (4)

KAREEM MAHAMMED
KAREEM MAHAMMED

Reputation: 1695

Use the different tags for Your UITableViews.

Upvotes: 0

vikingosegundo
vikingosegundo

Reputation: 52227

Yes, as the other posters shows, it is possible.
But there is another approach, where each TableView has it own controller. These controller get instantiated in another UIViewController. This is less coupled, so re-use ability is greater. For communication between the two UIViewController use delegation.
Have a look on my sample code.

Upvotes: 0

mmccomb
mmccomb

Reputation: 13807

Yes it is possible.

The differentiation is possible due to the method signatures of the UITableViewDelegate and UITableViewDataSource interfaces. If you look at either interface you'll notice that they both pass a reference to the UITableView instance for which the method was invoked...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

Check out the UITableViewDelegate and UITableViewDataSource API docs for more info.

Upvotes: 0

railwayparade
railwayparade

Reputation: 5156

Yes definately. Create two instance variables one for each tableview.

The table view delegate methods include a pointer to the table view calling them in the method do something like,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(tableView != [self tableView1]){
      //do stuff
    }
    //handle tableView2
    else{

    }

Upvotes: 2

Related Questions