我不知道如何按字母顺序对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];
请指出正确的方向。我的朋友还告诉我检查“ sortUsingSelector”方法...但是我仍然无法弄清楚。
我正在使用的示例代码位于here。
最佳答案
刚刚测试了示例代码,这就是我所做的。 NSSortDescriptor
将使用KVC,因此initWithKey
将是对象的相应属性。您的代码目前不执行任何操作,您正在尝试对空数组进行排序。此外,您不会修改表视图数据源。
- (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/