[英]selectRowAtIndexPath does not scroll in to the correct position in UITableView
我有大約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;
}
我剛遇到這個問題。 在我的情況下,我在定義所有行之前調用scrollToRowAtIndexPath(或selectRowAtIndexPath),即在所有行上調用cellForRowAtIndexPath之前。 我的解決方案是定義一個方法來選擇行並使用performSelectorOnMainThread調用它來延遲直到第一次刷新完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.