我有大约80件物品。如果我选择的索引是15(或更小的值),它会滚动到正确的位置。但如果选择指数为70,则不会滚动。但它在我手动滚动时选择了该行。有谁知道如何解决这个问题?
-(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;
}
答案 0 :(得分:1)
我刚遇到这个问题。在我的情况下,我在定义所有行之前调用scrollToRowAtIndexPath(或selectRowAtIndexPath),即在所有行上调用cellForRowAtIndexPath之前。我的解决方案是定义一个方法来选择行并使用performSelectorOnMainThread调用它来延迟直到第一次刷新完成。