2012-03-15 10 views
0

私のiphonアプリケーションでは、NSMutableArray * salesにsqlite dbを読み込んでおり、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]' の呼び出しを検討またはレンダリングされたすべての行に対してこれを呼び出すときに、View Controllerのinitメソッドで呼び出されます。それはおそらくあなたがパフォーマンスのために望むものではありません。

第2に、「販売」配列に含まれるものを確認し、その中にデータがあることを確認する必要があります。 indexPath.rowの値が配列のサイズを超えている場合、 'tableView:numberOfRowsInSection:'に実際の正しい行数が返されていない可能性があります。その場合、バッキングストアに存在しない可能性のあるデータを求められます。

また、UITableViewの使い方を「テーブルのセルにデータを割り当てる」ではなく、「現在表示されている特定のセルのデータを返す」と考えることもできます。

+0

ありがとうございます。やってみます。 – heziflash

+0

それは動作します。ありがとうございました – heziflash

関連する問題