2012-03-15 4 views
0

В моем приложении iphon я читаю от sqlite db в продажах NSMutableArray *, и я хочу назначить данные в продажах в ячейку в 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

Сначала рассмотрим вызова "[AppDelegate readSalesFromDatabase] на viewDidLoad или в вашем представлении метода инициализации контроллера, так как вы вызываете это для каждой строки рендеринга. Это, вероятно, не то, что вы хотите для производительности.

Во-вторых, вы должны проверить, что находится в массиве «sales» и убедиться, что в нем есть данные. Если значение indexPath.row превышает размер массива, скорее всего, вы не вернете фактическое и правильное количество строк в «tableView: numberOfRowsInSection:». В этом случае вас запрашивают данные, которые могут отсутствовать в вашем хранилище.

Кроме того, вы можете подумать о том, что ваш UITableView используется не как «назначение данных в ячейку таблицы», а вместо этого «возвращает данные для конкретной ячейки, которая отображается в настоящее время».

+0

спасибо. Я попробую. – heziflash

+0

Это работает. Еще раз спасибо – heziflash

Смежные вопросы