UITableView的核心数据按时间排序,而不是按字母顺序排序 [英] UITableView with Core Data ordered by time, not alphabetically
问题描述
我正在开发一个使用Core Data和UITableView的应用程序。我注意到的是,当我添加新的TableView条目时,单元格似乎按字母顺序排序。有没有一种方法可以使单元格按我添加它们的时间排序(从上到下),而不是按字母排序。谢谢!
I'm working on an app that uses Core Data and a UITableView. What I've noticed is that when I add a new TableView entry, the cells seem to order alphabetically. Is there a way to make it so the cells are ordered by the time I add them (from top to bottom) rather than by the alphabet. Thanks!
推荐答案
您应该为核心数据实体(类型NSDate)添加一个时间戳字段。然后,您可以在提取请求上使用简单的排序描述符对相应的实体进行排序。
You should add a timestamp field for your core data entity (type NSDate). Then, you can use a simple sort descriptor on your fetch request to sort the entities as appropriate.
例如,在创建实体之后...
For example, after you create the entity...
NSDate *now = [NSDate date];
[myNewEntity setValue:now forKey:@"timestamp"];
然后,在您的提取请求中...
and then, in your fetch request...
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"MyEntity"];
NSSortDescriptor *sortByTimestamp = [NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortByTimestamp];
这篇关于UITableView的核心数据按时间排序,而不是按字母顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!