核心数据排序使用UITableView和NSOrderedSet [英] Core Data ordering with a UITableView and NSOrderedSet

查看:209
本文介绍了核心数据排序使用UITableView和NSOrderedSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以Core Data作为后备存储的 UITableViewController 中实现用户定义排序需要执行哪些步骤?

What are the steps that I need to perform to implement user-defined ordering in a UITableViewController with Core Data as the backing store?

我还需要回应 -tableView:moveRowAtIndexPath:fromIndexPath:toIndexPath:由表视图自动处理排序?我可以检查核心数据模型文件中的有序复选框,并期望它将更改从表视图传输到商店,或者我还需要做更多?我必须改变我的 fetchedResultsController 获取它的对象(例如添加一个排序描述符)的方式吗?

Do I still need to respond to -tableView:moveRowAtIndexPath:fromIndexPath:toIndexPath: or is the model re-ordering handled automatically by the table view? Can I just check the "Ordered" checkbox in the Core Data model file and expect it to transmit the changes from the table view to the store, or do I have to do more? Do I have to change the way my fetchedResultsController gets it's objects (e.g. add a sort descriptor)?

推荐答案

您不需要放弃NSFetchedResultsController或NSOrderedSet 。如果你坚持使用NSOrderedSet,简单的方法是只是添加一个方法到你的对象NSManagedObject子类。因此,例如,如果你有一个对象Book有一套有序的Chapters,你可以添加一个方法到Chapters对象:

You don't need to abandon NSFetchedResultsController or NSOrderedSet. If you stick with NSOrderedSet, the easy way is to just add a method to your objects NSManagedObject subclass. So for instance, if you have an object Book with an ordered set of Chapters, you can add a method to the Chapters object:

- (NSUInteger)indexInBookChapters
{
    NSUInteger index = [self.book.chapters indexOfObject:self];
    return index;
}

提示:将此添加到类别,以便CoreData不会覆盖自动生成的代码。

Tip: Add this to a category so it doesn't get overwritten by CoreData auto generated code.

然后,您可以在您的NSFetchedResultsController的排序描述符中使用此方法:

Then you can use this method in your sort descriptor for your NSFetchedResultsController:

fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"indexInBookChapters" ascending:YES]];

这篇关于核心数据排序使用UITableView和NSOrderedSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆