問題描述
在使用 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:
還是模型重新排序由表視圖自動處理?我可以只選中 Core Data 模型文件中的Ordered"復選框并期望它將更改從 table view 傳輸到 store,還是我必須做更多?我是否必須更改 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,您可以向章節對象添加一個方法:
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 對核心數據進行排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!