[indexPath row]中的EXC_BAD_ACCESS

时间:2012-01-18 16:07:27

标签: iphone objective-c ios uitableview

我正在自定义我的表格单元格。我有这个代码(简化),在尝试访问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;
}

我做错了什么?

由于

2 个答案:

答案 0 :(得分:10)

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

因此您无法使用%@打印它。

你想要的是:

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

您遇到分段错误,因为%@用于指向对象的指针 当您传递一个整数时,NSLog将尝试在整数值指定的内存地址处打印一个对象。

答案 1 :(得分:0)

尝试此代码(重构一下)。您的错误在[indexPath row]上,返回int use(%i not%@)

-(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; }