简体   繁体   English

如何在使用多个时使用正确的fetchedResultsController

[英]How to use the correct fetchedResultsController when using more than one

I have a UIViewController with two UITableViews . 我有一个带有两个UITableViewsUIViewController 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.

相关问题 如何使用具有多个的componentsSeparatedByString - How to use componentsSeparatedByString with more than one 将故事板与多个视图控制器一起使用时,无法在.h中使用拖动功能 - Can't use the drag function in .h when using storyboard with more than one view controller 什么时候应该将deleteCacheWithName与fetchedResultsController一起使用? - When should I use deleteCacheWithName with fetchedResultsController? 对于iOS,何时一个应用程序使用多个窗口? - For iOS when would an App use more than one Window? 如何使用fetchedResultsController仅返回某些实体? - How to use fetchedResultsController to return only certain entities? Swift:当使用多个TableViewCell类时,如何在tableView上偏移索引? - Swift: How to offset index on tableView when using more than one TableViewCell class? 如何在Swift 4中使用FetchedResultsController获取DateSectionTitles - How to get DateSectionTitles using FetchedResultsController in Swift 4 如何将UIPicker与多个文本字段一起使用 - How use UIPicker with more than one text field 如何将SWRevealviewcontroller库用于多个视图控制器 - How to use SWRevealviewcontroller libraries for more than one view controller 如何在Core Plot中使用多个自定义标签? - How to use more than one custom labels with Core Plot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM