2012-03-15 96 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

相关问题