我正在使用NSFetchedResultsController填充应用程序的tableView,它是按节分组的(在这种情况下,我的核心数据对象中的category属性)是我想手动对节进行重新排序,而不是按字母顺序。

以下是我到目前为止所做的代码

- (NSFetchedResultsController *)fetchedResultsController {

if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Media" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"category" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

[fetchRequest setFetchBatchSize:20];

NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:_context sectionNameKeyPath:@"category"
                                               cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;

return _fetchedResultsController;

}

提前致谢!

最佳答案

如果任务是提供节的自定义顺序,则可以子类化NSFetchedResultsController并实现

- (NSInteger)sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)sectionIndex;

并且,如果需要
@property (nonatomic, readonly) NSArray *sectionIndexTitles;
- (NSString *)sectionIndexTitleForSectionName:(NSString *)sectionName;

关于objective-c - 对核心数据中的特定部分进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8559858/

10-13 03:56