问题描述
我有大约 80 件商品.如果我选择的索引是 15(或更少的值),它会滚动到正确的位置.但是,如果选择索引为 70,则它不会滚动.但是当我手动滚动时它选择了该行.有没有人知道如何解决这个问题?
Im having around 80 items. if my selected index was 15(or less value) it scrolls to the correct position. But if selection index was 70 it does not scroll. but it has selected the row when i manually scroll. Does anyone has idea how to fix that?
-(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;
}
推荐答案
我刚刚遇到了这个问题.就我而言,我在定义所有行之前调用了 scrollToRowAtIndexPath(或 selectRowAtIndexPath),即在对所有行调用 cellForRowAtIndexPath 之前.我的解决方案是定义一个方法来选择行并使用 performSelectorOnMainThread 调用它以延迟到第一次刷新完成.
I just had this problem. In my case I was calling scrollToRowAtIndexPath (or selectRowAtIndexPath) before all the rows were defined, i.e. before cellForRowAtIndexPath had been called on all the rows. My solution was to define a method to select the row and call it with performSelectorOnMainThread to delay until the first refresh was complete.
这篇关于selectRowAtIndexPath 不会滚动到 UITableView 中的正确位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!