Reputation: 987
I have a UIViewController
with two UITableViews
. The two tables are populated from different entities so I am using two fetched results controllers. When it comes to using the UITableViewDelegate/Datasource methods how do I designate which fetchedResultsController
to use?
How to filter NSFetchedResultsController (CoreData) with UISearchDisplayController/UISearchBar
The link above basically walks one through how to implement two FRC's but I'm new enough that I'm not sure how his method below works.
- (NSFetchedResultsController *)fetchedResultsControllerForTableView:(UITableView *)tableView
{
return tableView == self.tableView ? self.fetchedResultsController : self.searchFetchedResultsController;
}
Could someone spell this out for me? Specifically the return tableView == ....
line.
Thanks in advance!
Upvotes: 0
Views: 262
Reputation: 15449
There are two things you probably need to understand here.
First is, that this statement includes a ternary operator(the '?'). Ternary operators are for convenience programming, and that one line is exactly the same as:
if(tableView == self.tableView){
return self.fetchedResultsController;
} else {
return self.searchFetchedResultsController;
}
The second thing is understanding the callback. You are controlling two tableViews in your UIViewController, and they each use their own NSFetchedResultsController. When the fetchedResultsControllerForTableView callback is called by the system, you have to return the correct fetchedResultsController for the given table.
So the fetchedResultsControllerForTablView function in general is going to look something like this:
- (NSFetchedResultsController *)fetchedResultsControllerForTableView:(UITableView *)tableView
{
if(tableView == self.tableView1){
return self.fetchedResultsController1;
} else if (tableView == self.tableView2){
return self.fetchedResultsController2;
} else if ...
for n number of tables.
EDIT: This function is not called by the system, but its a helper method called in the user class. (I just re-read the link in the question). But the idea is essentially the same.
Let's take the numberOfSectionsInTableView function for example. That is called by the iOS system so that it knows how many sections to put in the tableView. The system passes the TableView that's in question into the function. Well, the number of sections is based on the information in the fetchedResultsController, but which fetchedResultsController do we use? The helper function takes the table (orginially passed into the numberOfSectionsInTableView function) and figures out which fetchedResultsController to use. Then we use that to answer the question "How many sections?"
Upvotes: 2