2012-07-24 53 views
0

我想用Xcode 4.3和SDK 5.1創建一個簡單的UITableView。在較舊的SDK中,我沒有收到以下錯誤,所以儘管檢查了Profiler和NSZombie,但對於發生的事情我有點困惑。UITableView didSeectRowAtIndexPath導致EXC_BAD_ERROR

在我.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方法:]:消息發送到釋放實例「

我簡要介紹了一下ARC,並認爲這可能是內存管理的問題,所以我刪除了dealloc,但它仍然會拋出異常。這個簡單的例子我錯過了什麼?

回答

0

該代碼看起來很完美。我猜ARC是禁用的,因爲你有autorelease等,即使你提到它。 我會檢查的一件事是UITableView的委託,看看它是否正確地設置到你的視圖控制器在xib上。

+0

我已檢查代表設置是否正確。它仍然只是在@autoreleasepool {return UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class]))中拋出錯誤; }在應用程序委託。 – fes 2012-07-25 06:40:09

+0

嗯我明白了。我實際上覆制和粘貼你的代碼並創建一個項目。在SDK 5.1中,它在Xcode 4.2中工作得很好。我不知道那麼 – mask8 2012-07-25 06:42:28

+0

我只是做了同樣的工作,所以一定有其他地方有什麼問題。我創建了一個簡單的登錄,當它成功將一個DefaultViewController推入棧中時,在這個DefaultViewController中就是這個簡單的UITableView所在的位置。我不確定爲什麼這會拋出異常,當我在創建的新項目中測試uitableview時,它不會像代碼一樣。 – fes 2012-07-25 06:59:31

相關問題