2012-01-18 8 views
2

テーブルセルをカスタマイズしています。私は私が間違っているのは何[indexPath row][indexPath行]のEXC_BAD_ACCESS

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier = @"WCFPictureCell"; 
    static NSString *CellNib = @"WCFPictureCell"; 
    WCFPictureCell *cell = (WCFPictureCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil]; 
     cell = (WCFPictureCell *)[nib objectAtIndex:0]; 
    } 
    NSLog(@"iph %@", indexPath); 
    NSLog(@"iphrow %@", [indexPath row]); 
    return cell; 
} 

にアクセスしようとしたときに私にEXC_BAD_ACCESSを与えている、このコード(簡易)を持っていますか?

おかげ

答えて

10

NSIndexPath戻りNSIntegerrow方法。
これはプリミティブ型であり、オブジェクトではありません。

したがって、%@を使用して印刷することはできません。何をしたい

は次のとおりです。

NSLog(@"iphrow %i", [ indexPath row ]); 

%@はオブジェクトへのポインタのために使用されているので、あなたがセグメンテーションフォールトを取得しています。
整数を渡すと、NSLogは整数値で指定されたメモリアドレスにオブジェクトを印刷しようとします。

+0

なぜあなたは%iを使用したのですか?私は常に整数の '%d'を使用していました。' NSInteger'は 'int'の単なるtypedefですが、好み以外に使う理由はありますか? –

+2

'%i'と'%d'は 'printf'と同じものです。両方とも符号付き整数に使用されます。それはちょうど習慣の問題です。しかし、 '%i'と'%d'は 'scanf'の意味が異なります。 – Macmade

+0

ありがとうございました。 +1 –

0

このコードを試してください(少しリファクタリングしてください)。あなたのエラーは[indexPath row]でintを返すものです(%iは%@ではありません)

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    WCFPictureCell *cell = (WCFPictureCell *)[tableView dequeueReusableCellWithIdentifier:@"WCFPictureCell"]; 
    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"WCFPictureCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 
NSLog(@"iph %@", indexPath); 
NSLog(@"iphrow %i", [indexPath row]); 
return cell; }