Yongzhi
Yongzhi

Reputation: 1070

(iphone) how to refer multiple tableview

Hi I have a view that contains 2 tableviews.

For a single view I know this delegate method can help me fill content of each row

But since I have 2 tableviews, how can I refer to each of them and fill content separately?

Thanks for helping!

Upvotes: 2

Views: 539

Answers (4)

AechoLiu
AechoLiu

Reputation: 18428

I prefer to use property tag for distinguishing the desired UIView. The UITableView is a subclass of UIView, so it have this property too.

Upvotes: 0

Ishu
Ishu

Reputation: 12787

since you have two table view so you need implement proper if else condition where you need which table view is going to display.Ok Make IBOutlet for both table.

then now in viewWillAppear

make datasource for the display(Array of data) and

if(...) { firstTable.hidden=NO; secondTable.hiden=YES; [firstTable reloadData]; } else { secondTable.hidden=NO; firstTable.hidden=YES; [[secondTable reloadData];

}

Now dont worry with every condition you would not require any condition in CellForRowAtIndexPath or didSelectRowAtIndexPath.

Upvotes: 1

mikecsh
mikecsh

Reputation: 862

Further to Toastor's answer above, you may of course set different delegates and data sources for each table, although most often it is simpler to use the same delegate/data source for all NSTableViews in the same view.

Upvotes: 0

Toastor
Toastor

Reputation: 8990

The "current" tableview will pass a pointer to itself along as an argument when calling its delegate's methods like the one you mentioned.

So, all you need to do is to compare the pointer (tableView) to references of the two tableviews that you stored or added as property previously.

Proceed like so within your delegate methods:

if (tableView == myFirstTableView) {
   //code to handle first tableview
} else if (tableView == mySecondTableView) {
   //code to handle second tableview
}

Edit: Both tableviews need to share the same delegate for this to work, which would make sense anyway since they appear on the same view.

Upvotes: 3

Related Questions