2011-10-17 28 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调用selectFirstRow,其调用selectRowAtIndexPath:animated:scrollPosition:,其调用tableView:numberOfRowsInSection无限。

您需要将您的selectFirstRow电话转至viewDidAppearviewWillAppeartableView:numberOfRowsInSection:是没有地方做任何复杂的事情......它被称为非常经常。

而当你在它的时候,将检查项目数量的逻辑移动到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?"); 
} 

它更干/模块化/清洁剂的方式。