2014-01-10 94 views
3

我有大約80個項目。如果我選擇的索引是15(或更小的值),它會滾動到正確的位置。但是,如果選擇索引是70,它不會滾動。但它在我手動滾動時選擇了該行。有沒有人有想法如何解決這個問題?selectRowAtIndexPath不滾動到正確的位置在UITableView

-(void)select:(NSString*)selectedIndex { 
    if(selectedIndex == nil){ 
     return; 
    } 
    UITableView *tableView = (UITableView*)view; 
    if(tableView == nil) { 
     return; 
    } 

    NSIndexPath *nextIndexPath = [self findIndexPath:selectedIndex table:tableView]; 

    if(nextIndexPath != nil && ![viewController isSelectable:[selectedIndex intValue]]){ 
     NSArray *indexPaths = [tableView indexPathsForVisibleRows]; 
     nextIndexPath = [self nextMoveUp:nextIndexPath :indexPaths]; 
    } 
    if(nextIndexPath != nil) { 
     [tableView selectRowAtIndexPath:nextIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 
     if ([tableView cellForRowAtIndexPath:nextIndexPath] == nil) { 
      // cell is not visible - scroll it into view 
      [tableView scrollToRowAtIndexPath:nextIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES]; 
     } 
    } 
} 

-(NSIndexPath*)findIndexPath:(NSString*)selectedIndex table:(UITableView*)tableView{ 
    NSArray *indexPaths = [tableView indexPathsForVisibleRows]; 
    if(indexPaths == nil || [indexPaths count] == 0) { 
     return nil; 
    } 
    NSIndexPath *lastIndexPath = NULL; 
    for (NSIndexPath *path in indexPaths) { 
     if(path.row == [selectedIndex intValue]){ 
      return path; 
     } 
     lastIndexPath = path; 
    } 
    if(lastIndexPath != nil && lastIndexPath.row < [selectedIndex intValue]){ 
     [tableView scrollToRowAtIndexPath:lastIndexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; 
     return [self findIndexPath:selectedIndex table:tableView]; 
    } 
    return nil; 
} 

回答

1

我剛纔有這個問題。在我的情況下,我在調用scrollToRowAtIndexPath(或selectRowAtIndexPath)之前定義了所有行,即在所有行上調用了cellForRowAtIndexPath之前。我的解決方案是定義一個方法來選擇行並使用performSelectorOnMainThread調用它來延遲,直到第一次刷新完成。

相關問題