[英]How to use the correct fetchedResultsController when using more than one
I have a UIViewController
with two UITableViews
. 我有一个带有两个
UITableViews
的UIViewController
。 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? 在使用UITableViewDelegate / Datasource方法时,如何指定要使用的
fetchedResultsController
?
How to filter NSFetchedResultsController (CoreData) with UISearchDisplayController/UISearchBar 如何使用UISearchDisplayController / UISearchBar过滤NSFetchedResultsController(CoreData)
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. 上面的链接基本上是通过如何实现两个FRC,但我是新的,我不知道他的方法如何工作。
- (NSFetchedResultsController *)fetchedResultsControllerForTableView:(UITableView *)tableView
{
return tableView == self.tableView ? self.fetchedResultsController : self.searchFetchedResultsController;
}
Could someone spell this out for me? 有人可以为我拼写这个吗? Specifically the
return tableView == ....
line. 具体来说就是
return tableView == ....
line。 Thanks in advance! 提前致谢!
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.
您在UIViewController中控制两个tableView,它们各自使用自己的NSFetchedResultsController。 When the fetchedResultsControllerForTableView callback is called by the system, you have to return the correct fetchedResultsController for the given table.
当系统调用fetchedResultsControllerForTableView回调时,您必须为给定的表返回正确的fetchedResultsController。
So the fetchedResultsControllerForTablView function in general is going to look something like this: 所以fetchedResultsControllerForTablView函数通常看起来像这样:
- (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. 对于n个表。
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. 我们以numberOfSectionsInTableView函数为例。 That is called by the iOS system so that it knows how many sections to put in the tableView.
这是由iOS系统调用的,因此它知道要放入tableView中的部分。 The system passes the TableView that's in question into the function.
系统将有问题的TableView传递给函数。 Well, the number of sections is based on the information in the fetchedResultsController, but which fetchedResultsController do we use?
那么,部分的数量是基于fetchedResultsController中的信息,但是我们使用了哪个fetchedResultsController? The helper function takes the table (orginially passed into the numberOfSectionsInTableView function) and figures out which fetchedResultsController to use.
辅助函数获取表(原始传递给numberOfSectionsInTableView函数)并确定要使用的fetchedResultsController。 Then we use that to answer the question "How many sections?"
然后我们用它来回答“多少部分?”的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.