1

我已經到了將這頭髮拉出來的地步。當我調用UITableView的reloadData時,我不斷收到EXC_BAD_ACCESS。我會在這裏爲你奠定基礎。UITableView reloadData上的EXC_BAD_ACCESS調用

MainViewController.h

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 

@class iPROSAppDelegate; 

@interface MainViewController : UIViewController <UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource> { 
iPROSAppDelegate *parent; 

... 

NSMutableArray *contentSeqList; 
UITableView *contentSeqTable; 
} 

@property (nonatomic, retain) IBOutlet iPROSAppDelegate *parent; 
... 
@property (nonatomic, retain) NSMutableArray *contentSeqList; 
@property (nonatomic, retain) IBOutlet UITableView *contentSeqTable; 
... 
- (IBAction)changeMainView:(id)sender; 

@end 

MainViewController.m當點擊在主視圖中的按鈕

- (IBAction)changeMainView:(id)sender{ 
    //do logs to make sure we get here and grab an NSDictionary object from another class 
NSLog(@"got to a change of main view"); 
NSDictionary *myContentSequences = [[parent csList] grabCSListandReturnJSON:[sender tag]]; 
NSLog(@"got cs list for tag %@: %@", [sender currentTitle], myContentSequences); 

    //set up table view 
contentSeqTable = [[UITableView alloc] initWithFrame:CGRectMake(40, 20, 240, 300) style:UITableViewStyleGrouped]; 
contentSeqTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
[contentSeqTable setDelegate:self]; 
[contentSeqTable setDataSource:self]; 
NSLog(@"delegate: %@, data source: %@", [contentSeqTable delegate], [contentSeqTable dataSource]); 
contentSeqList = [[NSMutableArray alloc] init]; 

    //populate nsmutablearray contentseqlist using the nsdictionary mycontentsequences 
for (id dashboard in [myContentSequences objectForKey:@"dashboards"]){ 
    if ([dashboard objectForKey:@"error"] == nil){ 
    for (id contentsequence in [dashboard objectForKey:@"contentSequences"]){ 
    [contentSeqList addObject:[contentsequence objectForKey:@"name"]]; 
    NSLog(@"added object to content sequence list: %@", [contentsequence objectForKey:@"name"]); 
    } 
    } 
} 
[contentSeqTable reloadData]; 
... 
} 

changeMainView被調用。以下是UITableView的委託方法。請注意,NSLogs是這些方法的第一行,但在EXC_BAD_ACCESS之前,我沒有在控制檯日誌中顯示。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
NSLog(@"content seq list count: %@", [contentSeqList count]); 
return [contentSeqList count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
NSLog(@"setting up the cells"); 
NSUInteger index = [indexPath indexAtPosition:1]; 

NSString *myId = @"id"; 
if (tableView.editing) myId = @"idE"; 

UILabel *name; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myId]; 

if (cell == nil){ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 20) reuseIdentifier:myId] autorelease]; 

    float width = 260; 
    if (tableView.editing) width = 240; 

    name = [[[UILabel alloc] initWithFrame:CGRectMake(45, 1, width, 53)] autorelease]; 
    [name setBackgroundColor:[UIColor clearColor]]; 
    [name setTextColor:[UIColor blackColor]]; 
    [name setFont:[UIFont fontWithName:@"Helvetica" size: 18]]; 
    [name setAdjustsFontSizeToFitWidth:YES]; 
    [name setMinimumFontSize:11]; 
    [name setNumberOfLines:2]; 

    name.tag = 1; 

    [cell.contentView addSubview:name]; 

} 
else{ 
    name = (UILabel *)[cell.contentView viewWithTag:1]; 
} 

if ([tableView indexPathForSelectedRow]){ 
    if (indexPath == [tableView indexPathForSelectedRow]){ 
    [name setTextColor:[UIColor whiteColor]]; 
    } 
    else{ 
    [name setTextColor:[UIColor blackColor]]; 
    } 
} 
else{ 
    [name setTextColor:[UIColor blackColor]]; 
} 

[name setText:[contentSeqList objectAtIndex:index]]; 

return cell; 
} 

委託方法是同一個類MainViewController的一部分。我也在reloadData之前檢查過UITableView不是通過NSLog。我知道我必須在這裏錯過一些小東西。任何建議將不勝感激。謝謝!

回答

2

EXC_BAD_ACCESS發生時,您能顯示調用堆棧嗎?

順便說一句,

NSUInteger index = [indexPath row]; 

NSUInteger index = [indexPath indexAtPosition:1]; 
+0

哇更可讀,我完全忘記檢查從調試器調用堆棧。感謝您的建議,這是NSLog中numberOfRowsInSection的一個問題:我在嘗試執行%@時應該使用%d。謝謝! – 2009-11-17 15:02:19

+4

恨我的調試代碼導致錯誤... – wkw 2009-11-18 12:50:41

+3

你也可以使用indexPath.row ... – 2011-11-06 00:00:13