2011-07-26 10 views
2

このコードでは、奇妙な問題が発生しています。NSMutableArrayからUITableViewを取り込む

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    // Configure the cell... 
    if (accounts != nil) { 
     NSLog(@"Cell: %@", indexPath.row); 
     cell.textLabel.text = [self.accounts objectAtIndex: indexPath.row]; 
    } 
    else 
    { 
     NSLog(@"No cells!"); 
     [cell.textLabel setText:@"No Accounts"]; 
    } 

    return cell; 
} 

私のテーブルビューは、すべての行が私のNSMutableArrayaccounts内の最初の項目が含まれている以外、うまく読み込まれます。私はindexPath.rowの値を記録しており、配列にいくつの値が入っていても(null)にとどまります。私はここで何か間違っていますか?

答えて

3

私はこれを信じません!私は早くこれを見つけられないために頭の中で自分自身をボンディングしています!

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [accounts count]; //<--This is wrong!!! 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; // <--This needs to be switched with the error above 
} 

上記のコードは、それが二回私の配列内の同じ行を印刷するのではなく、私の列に前方に行進た理由でした。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [accounts count]; 
} 

このコードは正しく入力され、適切な結果が得られます。どんなふしだらな男だ。 ^^;

+0

cellForRowAtIndexPathが一度だけ呼び出されているかどうかについては、numberOfRowsを確認してください:) –

2

@"%i", indexPath.rowない@"%@", indexPath.row

また、私はあなたのメソッドの先頭でこれを置くことをお勧めする必要があります。

NSUInteger row = [indexPath row]; 

次に、あなたの方法は次のようになります。

// Cell Ident Stuff 
// Then configure cell 
if (accounts) { 
    NSLog(@"Cell: %i", row); 
    cell.textLabel.text = [self.accounts objectAtIndex:row]; 
} 
else { 
    NSLog(@"No accounts!"); 
    // Only setting for the first row looks nicer: 
    if (row == 0) cell.textLabel.text = @"No Accounts"; 
} 

それは時に良い習慣ですテーブルビューメソッドを扱う。それを試してください。

+0

私はこの変更を行ったので、NSLogは「Cell:0」だけを報告しています。それでも私の配列を通して進んでいくわけではありません。 – Tanoro

関連する問題