2014-10-15 2 views
2

매우 간단한 응용 프로그램에서 EXC_BAD_ACCESS 오류가 발생하므로 확실한 내용을 빠뜨려야합니다.오류 scrollViewDidScroll이있는 EXC_BAD_ACCESS

나는 내비게이션 컨트롤러 안에 첫 번째보기 컨트롤러가 있습니다. 거기에서 UITableView를 사용하여 두 번째보기 컨트롤러를 푸시합니다. 이 두 번째보기 컨트롤러에서 scrollViewDidScroll 대리자 메서드를 구현했습니다.

테이블 맨 아래로 스크롤 한 다음 첫 번째보기 컨트롤러로 돌아갈 때 EXC_BAD_ACCESS 오류가 발생합니다. 여기

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.view.backgroundColor = [UIColor redColor]; 
    UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 50)]; 
    button.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
    [button setTitle:@"Push tableView" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:button]; 
} 

- (void) buttonClick { 
    [self.navigationController pushViewController:[ViewController2 new] animated:YES]; 
} 

@end 

내가 scrollViewDidScroll 방법을 언급 할 때 에러가 발생하지

@interface ViewController2() <UITableViewDataSource, UITableViewDelegate> 

@property (nonatomic, strong) UITableView* tableView; 

@end 

@implementation ViewController2 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds]; 
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    self.tableView.dataSource = self; 
    self.tableView.delegate = self; 
    [self.view addSubview:self.tableView]; 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 20; 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    NSLog(@"scroll"); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString* identifer = @"identifer"; 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifer]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer]; 
    } 
    cell.textLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row]; 
    return cell; 
} 

@end 

번째 뷰 컨트롤러의 코드, 또는 다음은

첫번째 뷰 컨트롤러의 코드 첫 번째보기 컨트롤러로 돌아 가기 전에 테이블 끝까지 스크롤하지 않습니다.

iOS SDK 8이 설치된 XCode 6을 사용 중이며 iOS 7 및 iOS 8 시뮬레이터와 물리적 장치에서 테스트를 마쳤습니다.

이 말이 맞는가요?

답변

3

데이터 원본을 nil로 설정하고 UITableView의 속성을 두 번째보기 컨트롤러의 dealloc 메서드에 위임해야합니다. UITableView는 이미 할당이 취소 된 후 대리인에게 메시지를 보냅니다.

그래서, 두 번째 뷰 컨트롤러에서이 같은 :

-(void)dealloc 
{ 
    self.tableView.dataSource = nil; 
    self.tableView.delegate = nil; 
} 
+0

애플이'weak'에 이러한 속성을 업데이트하지 않았는지 난 그냥 이해가 안 돼요! – Aleksa