I have a UIView
that will need to display two UITableViews, but they are never shown together, by using a SegementedBar
you can toggle one or the other.
What would be the best way to handle this? Just create one Table View Controller
and change the data source, or create 2 Table View Controllers
and just hide one when the other is visible.
The 2 tables will have a completely different layout with different custom cells.
I would keep one datasource & delegate.
This means that all the delegate/datasource methods become more complicated BUT it means that you can retain the one to one relationship between viewController & view.
keep a reference to each of the table views
//vc.h
@property (nonatomic, weak) IBOutlet UITableView* firstTableView;
@property (nonatomic, weak) IBOutlet UITableView* secondTableView;
In the datasource/ delegate methods you need to account for the fact that the method needs to behave differently depending on which table view is in use. e.g.
//vc.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (tableView == self.firstTableView) {
...
} else { // tableView == self.secondTableView
...
}
}
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (tableView.tag == 1) {
...
} else { // tableView == self.secondTableView
...
}
}
tag could be assigned from the .xib. so no need to have UITableVeiw variable in .h file. Two table view in .xib needed
Both approach has some pros and cons, but i will personally prefer approach having two separate controller.
Approach 1 - create one Table View Controller and change the data source
Approach 2 - 2 Table View Controller
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With