单元格自动释放和“无法识别的选择器发送到实例”

时间:2012-12-12 11:48:50

标签: iphone ios uitableview

可能我的代码在以下方法中崩溃,因为getData()只被调用一次 - 在此方法结束时。

此外,didSelectRowAtIndexPath代码如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
{
    [tableView deselectRowAtIndexPath:newIndexPath animated:YES];

    // Set 
    [[GlobalObject obj] setData:[(CustomCell*)[tableView cellForRowAtIndexPath:newIndexPath] getData]];
}

由于未捕获的异常而终止应用'NSInvalidArgumentException',原因:' - [CustomCell getData]:无法识别的选择器发送到实例

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    if(indexPath.section == 0)
    {
        ...
        return cell;
    }
    else
    {
        // Create a button table view cell
        CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
        if (cell == nil) 
        {           
            cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomCell"] autorelease];
        }

         [cell setButton:[ ...];

        return cell;
    }
}

如上所示,单元格是自动释放的,这可能就是应用程序崩溃的原因。 Cell转到自动释放池,然后将getData发送到无法识别的选择器。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

CustomCell没有一个名为getData的方法 避免内联,一个接一个地写出语句。这有助于调试。然后,您可以轻松设置断点。