ios - 使用 sqlite 数据按字母顺序对 UITableView 进行排序

标签 ios sqlite uitableview sorting ios5

我不知道如何按字母顺序对 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/

相关文章:

ios - 在宏中使用时使用未声明的标识符 FLT_MAX

ios - Popoverview 不以 iPad 旋转为中心

iphone - 核心数据 VS Sqlite 或 FMDB……?

java - 按字母顺序对数据库中的数据进行排序?

objective-c - 在不同的 TableView 之间切换

swift - 搜索栏 textDidChange 错误

ios - 如何在 iOS 应用程序中集成 Facebook 用户的 Newsfeed(或)Wall post

SQLite - 复杂查询

ios - UITableviewcell 不显示 UICollectionView

ios - 使用 RxSwift 从后台进入前台时 AVPlayer 自动播放