iphone - NSFetchedResultController 多重限制和排序

标签 iphone ios core-data nsfetchedresultscontroller

你好,我的核心数据表有问题,类似这样

+-----------------------+
|person                 |
+-----------------------+
|name                   |
|address                |
|birthDate              |
|gender                 |
+-----------------------+

在我的 FetchedResultController 中,我通过性别 MaleFemale 将其分成两部分。 问题是我希望 Male 部分按其 birthDate 排序并且只限制 5 行,在 Female 部分我想按其 name 和 No Limit.

我可以在一个 FetchedResultController 中完成吗,或者我需要 2 个 FetchedResultController。或者如果有其他方法可以让我知道。

谢谢

最佳答案

如果您定义自己的排序方法,您可以使用一个 NSFetchedResultController 来完成。它会是这样的:

- (NSString *) sortBy
{ return (Male == self.gender ? self.birthDate : self.name); }

然后在 Controller 的配置中提供sortBy。请注意,正如@Martin R 指出的那样,您可能无法使用 Objective-C 方法进行排序;如果不是,则定义另一个属性 'sortOrder' 并在对象创建时计算它。

至于限制,不确定,但也许可以在 tableView:numberOfRowsInSection: Apple 给出这个例子:

- (NSInteger) tableView: (UITableView *) table 
  numberOfRowsInSection: (NSInteger) section
{
    id <NSFetchedResultsSectionInfo> sectionInfo = 
      [[<#Fetched results controller#> sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

当该部分是“男性”部分时,也许您可​​以返回“3”。

关于iphone - NSFetchedResultController 多重限制和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16351710/

相关文章:

ios - sinch api可以和swift一起使用吗

ios - 什么时候应该将 translatesAutoresizingMaskIntoConstraints 设置为 true?

ios - 核心数据 NSPredicate 不返回两个日期之间的记录

ios - 可靠地传送 iOS VOIP 推送通知

iphone - 在AppStore提交应用后如何更改内容?

ios - iTunesConnect 截图顺序困惑

ios - 如何在 Swift 中的 NSCoding 初始化时返回预先存在的核心数据对象?

ios - 将 Objective C 协议(protocol)与 Swift 集成

ios - 用于删除 UICollectionViewCells 的类似跳板的动画

objective-c - 无法识别的选择器 : [NSSQLToMany _setInverseManyToMany:]