我正在尝试使用 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;
}
谢谢(: