2012-03-15 54 views
0

在我的iphon應用程序中,我正在從sqlite數據庫讀入NSMutableArray * sales,並且想要將銷售數據分配給TableView中的單元格。 我該怎麼辦?如何將對象分配給TableView中的單元格?

這裏是我的代碼: 在控制器:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *result = nil; 
    if ([tableView isEqual:self.myTableView]){ 
     static NSString *TableViewCellIdentifier = @"MyCells"; 
     result = [tableView dequeueReusableCellWithIdentifier:TableViewCellIdentifier]; 
     if (result == nil){ 
     result = [[UITableViewCell alloc] 
        initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:TableViewCellIdentifier]; 
     } 

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    [appDelegate readSalesFromDatabase]; 

    // ***here is where I'm trying to retrive the data*** 
    // when I run the simulator, at this point I receive 'SIGABRT' 
    =====> result = [sales objectAtIndex:indexPath.row]; 
    } 
    return result; 
} 

在代表:

-(void) readSalesFromDatabase { 

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
    // Setup the SQL Statement and compile it for faster access 

      const char *sqlStatement = "select us.userID from UsersSale us order by us.saleID";   


    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) { 
      // Read the data from the result row 
      NSInteger auserID = sqlite3_column_int(compiledStatement, 0); 

      // Create a new Sale object with the data from the database     
      SelectFromList *sfl = [[SelectFromList alloc] initWithName:auser];           

      // ***here is where I'm inserting the data to NSMutableArray *sales ** 
      [selectFromListController.sales insertObject:sfl atIndex:count]; 

      [sfl release]; 

     } 
    } 
    // Release the compiled statement from memory 
    sqlite3_finalize(compiledStatement); 

} 
sqlite3_close(database); 

} @end

回答

0

首先,考慮呼籲viewDidLoad中 '[的appDelegate readSalesFromDatabase]'或者在您的視圖控制器的init方法中,因爲您正在爲呈現的每一行調用此方法。這可能不是你想要的表現。

其次,您應該檢查「銷售」數組中的內容,並確保其中包含數據。如果indexPath.row值超出數組的大小,那麼很可能是您沒有返回'tableView:numberOfRowsInSection:'中實際正確的行數。在這種情況下,系統會要求您提供可能不存在於您的後備商店中的數據。另外,您可能想要將您的UITableView用法看作「將數據分配到表格的單元格」,而不是「返回當前正在呈現的特定單元格的數據」。

+0

謝謝。我會嘗試。 – heziflash 2012-03-17 09:13:58

+0

它的工作原理。再次感謝你 – heziflash 2012-03-18 08:58:28

相關問題