2012-04-10 4 views
3

iPhone/iPod 앱을 개발 중입니다. 다음 코드는 UIViewController의 .m 파일입니다. 나는 다음과 같은 얻을 :iOS - EXC_BAD_ACCESS 오류

cell.textLabel.text = [datasource objectAtIndex:indexPath.row]; 

나는 당신이 당신이 그것을 발표 후 개체에 액세스하려고 할 때이 일반적으로 발생하는 것을 이해하지만, 나는 그것을 공개하지 않는 : 나는 다음과 같은 라인에 충돌

Thread 1: EXC_BAD_ACCESS (code=2...... 

내가 액세스하려고하기 전에. 아래에 전체 코드를 첨부했습니다.

도움이 감사합니다!

#import "HomePage.h" 
#import "HusbandryRecordsMain.h" 
#import "TaskManagerMain.h" 
#import "AnimalInventoryMain.h" 
#import "FeedInventoryMain.h" 

@implementation HomePage 
@synthesize options, datasource; 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad { 
    [self setupArray]; 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view, typically from a nib. 
} 

-(void)setupArray{ 
    options = [NSMutableArray arrayWithObjects:@"Husbandry Records", @"Task Manager", @"Feeder Inventory", @"Animal Inventory", nil]; 

    datasource = options; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    //#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return 4; 
} 

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

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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

    [[cell textLabel] setBackgroundColor:[UIColor clearColor]]; 
    [[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]]; 

    // THE FOLLOWING LINE IS THROWING THE ERROR! 
    cell.textLabel.text = [datasource objectAtIndex:indexPath.row]; 

    //Arrow 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (indexPath.row == 0){ 
     HusbandryRecordsMain *hrm = [self.storyboard instantiateViewControllerWithIdentifier:@"Husbandry Records - Main"]; 
     [self.navigationController pushViewController:hrm animated:YES]; 
    } 
    else if (indexPath.row == 1){ 
     TaskManagerMain *tmm = [self.storyboard instantiateViewControllerWithIdentifier:@"Task Manager - Main"]; 
     [self.navigationController pushViewController:tmm animated:YES]; 
    } 
    else if (indexPath.row == 2){ 
     FeedInventoryMain *fim = [self.storyboard instantiateViewControllerWithIdentifier:@"Feeder Inventory - Main"]; 
     [self.navigationController pushViewController:fim animated:YES]; 
    } 
    else if (indexPath.row == 3){ 
     AnimalInventoryMain *aim = [self.storyboard instantiateViewControllerWithIdentifier:@"Animal Inventory - Main"]; 
     [self.navigationController pushViewController:aim animated:YES]; 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

//----------------------TABLEVIEWCELL HEIGHT ------------------------------------------- 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    return 70; 

} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 
+4

매우 강력한 이유가없는 한 ARC를 사용하십시오. – zaph

+1

비행을 배우는 중이라면 호를 사용하지 마십시오. – NCFUSN

답변

0

파일 옵션과 데이터 소스의 유형은 무엇? 또한 self.options 등을 시도하십시오.

+0

datasource는 NSArray이고 옵션은 NSMutableArray – comead

+0

입니다. 왜 데이터를 보존하기 위해 두 개의 배열이 필요합니까? 당신은 아마 그들을 (강한 또는 보유) 유지했습니다. self.option 만 사용하고 dealloc 메소드에서 반드시 해제해야합니다. – NCFUSN

+0

return 4; 반환 할 수 있습니다 [self.option count]; – NCFUSN

5
-(void)setupArray{ 
    options = [NSMutableArray arrayWithObjects:@"Husbandry Records", @"Task Manager", @"Feeder Inventory", @"Animal Inventory", nil]; 

    datasource = options; 
} 

사용자가 직접 datasource 인스턴스 변수에 오토 릴리즈 객체를 할당하고이 충돌하는 코드 줄에서 출시 이후를 사용하려고합니다. 좀비 탐지 기능을 켜면 직접 잡았을 가능성이 큽니다. 또한 정적 분석기 (빌드 및 분석)가이를 감지해야합니다.

(... 물론, 당신은 ARC이 진행되는 시점 뭔가 다른 활성화 한 경우 제외)

+0

좀비 검색이란 무엇입니까? 나는 이것에 익숙하지 않다. ARC가 꺼져 있습니다. 계속 켜 두어야합니까? 그리고, 좋아, 그럼 어떻게 고칠 수 있니? – comead

+1

좀비 탐지는 프로필 도구의 도구입니다. – NCFUSN

+2

옵션을 '보유'해야합니다. 'self.datasource = options;'또는'datasource = [options retain];'을 사용하여. 새로운 코드 인 경우 ARC 전체를 사용해야합니다. 그러나 실제로 Objective-C 및 메모리 관리의 기본 사항을 배우거나이 문제가 많은 문제의 첫 번째가 될 것입니다/두통을 일으키는 크래셔 (실행하기 전에 걸으십시오). – bbum