0

我正在尝试使用 Xcode 4.3 和 SDK 5.1 创建一个简单的 UITableView。在较旧的 SDK 中,我没有收到以下错误,因此尽管检查了 Profiler 和 NSZombie,但我对正在发生的事情感到有些困惑。

在我的 .h 文件中

NSMutableArray *fruits;

在我的 .m 文件中

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    fruits = [[NSMutableArray alloc] init];

    [fruits addObject:@"Apples"];
    [fruits addObject:@"Bananas"];
    [fruits addObject:@"Oranges"];
    [fruits addObject:@"Kiwi Fruit"];
}

- (void)dealloc
{
    [fruits release];
    [super dealloc];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%d", [fruits count]);
    return [fruits count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

    NSString *title = [fruits objectAtIndex:indexPath.row];
    cell.textLabel.text = title;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *selectedOption = [fruits objectAtIndex:indexPath.row];
    NSLog(@"%@", selectedOption);
}

每当我选择一个水果时,我都会收到 EXC_BAD_ERROR 并且输出显示“tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance”

我简要了解了 ARC,并认为这可能是内存管理的问题,所以我删除了 dealloc,但它仍然抛出异常。我从这个简单的例子中遗漏了什么?!

4

1 回答 1

0

代码看起来很完美。我猜 ARC 被禁用了,因为你有 autorelease 等,即使你提到了它。我要检查的一件事是 UITableView 的委托,以查看它是否正确设置为 xib 上的视图控制器。

于 2012-07-25T00:36:40.073 回答