Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple delegates/datasources in UITableView

Objective-c delegates entertain one instance at a time like if I have two view controllers and both implementing same delegate, but only currently presented view controller receive callbacks.

If I have a two uitableviews in same view controller both uitableview's delagates datasource are set like

tb1.datasource = self; 
tb2.datasource = self;

tb1.delegate = self; 
tb2.delegate = self;

How uitableview's working with delegates defined at same time?

like image 930
S.J Avatar asked Mar 20 '26 02:03

S.J


1 Answers

Your delegate/datasource methods will be called twice, once for every table, and you can detect which table was called from:

For example:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == tb1)
        return 10;
    else if (tableView == tb2)
        return 8;
}

That's the reason the caller tableView is passed as a variable in the methods, so you can detect where it's coming from.

like image 79
Antonio MG Avatar answered Mar 21 '26 19:03

Antonio MG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!