2011-10-17 6 views
1

뷰가로드 될 때 테이블 뷰의 첫 번째 행을 선택하는 메서드를 호출하고 있습니다. 그러나 어떤 이유로 인해 selectFirstRow이 호출 된 후 self.couldNotLoadData = NO으로 돌아가고 계속 이동합니다. 어떤 아이디어? 초기 if/else 루프가 else로 이동하면 해당 메소드가 호출되지 않으므로 루핑을 유지하지 않습니다.UITableView에서 무한 루프가 발생하는 이유는 무엇입니까?

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (self.ichronoAppointments.count > 0) 
    { 
     self.couldNotLoadData = NO; 
     [self selectFirstRow]; 
     return self.ichronoAppointments.count; 
    } 
    else 
    { 
     self.couldNotLoadData = YES; 
     return 1; 
    } 
} 
-(void)selectFirstRow 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; 
} 

답변

1

이 확인되지 않은,하지만 난 당신이 selectFirstRow에서 selectRowAtIndexPath:animated:scrollPosition:를 호출 할 때 UITableView의 대리인의 -tableView:numberOfRowsInSection: 호출 내기.

기본적으로 무한 재귀가 발생합니다. tableView:numberOfRowsInSection은 을 호출하며 selectRowAtIndexPath:animated:scrollPosition:을 호출하며 tableView:numberOfRowsInSection을 무한으로 호출합니다.

또는 viewWillAppear으로 전화를 걸어 야합니다. tableView:numberOfRowsInSection:은 복잡한 작업을 수행 할 수있는 곳이 아닙니다 ... 자주 VERY라고합니다.

그리고 여기에있는 동안 항목 수를 확인하는 로직을 selectFirstRow로 이동하십시오. 즉

if (self.ichronoAppointments.count) { 
    //select the first row 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; 
} else { 
    //don't 
    NSLog(@"Couldn't select first row. Maybe the data is not yet loaded?"); 
} 

그런 식으로 드라이/모듈러/클리너입니다.

관련 문제