【问题标题】:How to assign from object to a cell in TableView?如何从对象分配给 TableView 中的单元格?
【发布时间】:2012-03-15 14:56:20
【问题描述】:

在我的 iphon 应用程序中,我正在将 sqlite db 读取到 NSMutableArray *sales 中,并且我想将 sales 中的数据分配到 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);

} @结束

【问题讨论】:

    标签: ios5 sqlite xcode4.2


    【解决方案1】:

    首先,考虑在 viewDidLoad 或视图控制器的 init 方法中调用“[appDelegate readSalesFromDatabase]”,因为您正在为渲染的每一行调用它。这可能不是您想要的性能。

    其次,您应该检查“sales”数组中的内容,并确保其中有数据。如果 indexPath.row 值超过了数组的大小,则很可能您没有返回 'tableView:numberOfRowsInSection:' 中实际且正确的行数。在这种情况下,系统会要求您提供后备存储中可能不存在的数据。

    此外,您可能不希望将 UITableView 的使用视为“将数据分配到表格的单元格”,而是“返回当前正在呈现的特定单元格的数据”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 2022-09-25
      • 2021-01-22
      相关资源
      最近更新 更多