sqlite的记录不会显示为iPhone
问题描述:
我只能显示出10条记录sqlite的记录不会显示为iPhone
qlite3 *database;
scores = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "select name,score from game";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
//if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
Book * aBook = [[Book alloc] init];
// NSString *id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
aBook.id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
aBook.name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
SQLiteTutorialAppDelegate *appDelegate = (SQLiteTutorialAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.books = [[NSMutableArray alloc] init];
[books addObject:aBook];
// aBook.name shows all 10 names
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
现在
最后elemnts如果我把这个像cell.text=aBook.name
表视图,以便只显示最后一个元素
答
在每次迭代你正在为appDelegate.books创建一个新的数组,然后插入一本书。这就是循环appDelegate.books仅包含最后一个元素之后的原因。
只在循环之前创建appDelegate和appDelegate.books一次。
作为回答 您很可能将每个单元格的文本设置为同一本书。
你说你使用'cell.text = aBook.name'。
你是如何得到'aBook.name'的。您将需要使用indexPath像
Book* aBook = [appDelegate.books objectAtIndex:indexPath.row];
嘿,我就照你说的,但现在我可以看到所有的10个记录的同名plz帮助 我这样做之前,while循环书* ABOOK = [[书页头] INIT ]。 \t \t SQLiteTutorialAppDelegate * appDelegate =(SQLiteTutorialAppDelegate *)[[UIApplication sharedApplication] delegate]; \t \t appDelegate.books = [[NSMutableArray alloc] init]; – ram 2010-07-07 01:14:03
完美ü拯救了我的生命,以你的知识帽子:-) – ram 2010-07-07 01:25:49