2010-07-07 41 views
0

我只能顯示出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表視圖,以便只顯示最後一個元素

回答

2

在每次迭代你正在爲appDelegate.books創建一個新的數組,然後插入一本書。這就是循環appDelegate.books僅包含最後一個元素之後的原因。

只在循環之前創建appDelegate和appDelegate.books一次。

作爲回答 您很可能將每個單元格的文本設置爲同一本書。

你說你使用'cell.text = aBook.name'。

你是如何得到'aBook.name'的。您將需要使用indexPath像

Book* aBook = [appDelegate.books objectAtIndex:indexPath.row]; 
+0

嘿,我就照你說的,但現在我可以看到所有的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

+0

完美ü拯救了我的生命,以你的知識帽子:-) – ram 2010-07-07 01:25:49