我正在尝试使用SQLite来保存我的应用程序的数据。该数据库中可能有成千上万条记录,因此在启动时将它们全部加载到阵列中可能不是一个好主意。因此,我分别通过id将它们分别加载到cellForForAtIndexPath中。这行得通,但我无法对它们进行排序...有人能想到更好的方法吗?我敢打赌,这真的很简单:L

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"VerbCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    VerbData *verb = [[VerbData alloc] init];
    if(isFiltered)
        verb = [searchResults objectAtIndex:indexPath.row];
    else
    {
        const char *sql = [[NSString stringWithFormat: @"SELECT infinitive, english FROM verbs WHERE id=%d", indexPath.row+1] UTF8String];
        sqlite3_stmt *sqlStatement;
        if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK){
            NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
        }else{
            while (sqlite3_step(sqlStatement) == SQLITE_ROW) {
                verb.infinitive = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,0)];
                verb.english = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sqlStatement,1)];
            }
        }
    }

    cell.textLabel.text = verb.infinitive;
    cell.detailTextLabel.text = verb.english;

    return cell;
}


谢谢(:

最佳答案

您可以使用偏移量和限制顺序选择

Row Offset in SQL Server

当您使用ORDER BY [ASCENDING |降序]所有排序工作将由DBMS进行,如果您有远程数据库,这对移动设备非常有用,因此服务器端工作量很大。您也可以使用

选择...从...订购...偏移量限制

哪里:
offset-从数据库开始偏移
限制-是查询收集的最大数据行数

然后,您可以分块加载表。另一种技巧是在表滚动时异步查询行,即加载前100行,但是当用户在第70行以下时,将异步进行另一个查询并将数据添加到数据源数组

10-06 06:35
查看更多