我不知道如何按字母顺序对 UITableView 中的数据进行排序。
我一直在使用 Icodeblog ( Website) 中的 ToDoList 示例。一旦我输入数据,它就会出现在 UITableview 上。数据位于 sqlite 文件中。
我在 UITableview 栏中创建了一个按钮来运行此代码以按字母顺序对文本进行排序...但它不起作用:
NSMutableArray *array = [NSMutableArray array];
NSArray *sortedArray;
NSSortDescriptor *lastDescriptor =
[[NSSortDescriptor alloc] initWithKey:newTodo.text
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObjects:lastDescriptor, nil];
sortedArray = [array sortedArrayUsingDescriptors:descriptors];
[self.tableView reloadData];
请指出正确的方向。我的 friend 还告诉我检查“sortUsingSelector”方法......但我还是想不通。
我使用的示例代码位于 here .
最佳答案
刚刚测试了示例代码,这就是我对它所做的。 NSSortDescriptor
将使用 KVC,因此 initWithKey
将是对象的相应属性。您的代码目前没有执行任何操作,您正在尝试对一个空数组进行排序。此外,您没有修改 TableView 数据源。
- (void)viewWillAppear:(BOOL)animated
{
todoAppDelegate *appDelegate = (todoAppDelegate *)[[UIApplication sharedApplication] delegate];
NSSortDescriptor *lastDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"text"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *descriptors = [NSArray arrayWithObjects:lastDescriptor, nil];
NSMutableArray* sortedArray = [[[appDelegate.todos sortedArrayUsingDescriptors:descriptors] mutableCopy] autorelease];
appDelegate.todos = sortedArray;
[self.tableView reloadData];
[super viewWillAppear:animated];
}
关于ios - 使用 sqlite 数据按字母顺序对 UITableView 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11004831/