[indexPath row]中的EXC_BAD_ACCESS

Mar*_*arc 2 iphone objective-c uitableview ios

我正在定制我的表格单元格.我有这个代码(简化),它EXC_BAD_ACCESS在试图访问时给了我一个[indexPath row]

-(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;
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

谢谢

Mac*_*ade 10

返回的row方法NSIndexPatha NSInteger.
那是一种原始类型,而不是一个对象.

所以你不能用它打印%@.

你想要的是:

NSLog( @"iphrow %i", [ indexPath row ] );
Run Code Online (Sandbox Code Playgroud)

您正在获得分段错误,因为%@它用于指向对象的指针.
在传递整数时,NSLog将尝试在整数值指定的内存地址处打印对象.

  • `%i`和`%d`与`printf`是一回事.两者都用于有符号整数.这只是一个习惯问题.但请注意`%i`和`%d`在`scanf`中有不同的含义. (2认同)