首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >iOS UItableview scrollToRowAtIndexPath不再工作

iOS UItableview scrollToRowAtIndexPath不再工作
EN

Stack Overflow用户
提问于 2012-09-20 22:24:32
回答 8查看 29.1K关注 0票数 23

今天早上我刚安装了新的Xcode,其中包括iOS 6。

我有一个加载了plist文件的表视图,其中包含章节和行。章节定义了各个部分。

用户选择章节和行,表格视图自动滚动到正确的位置(在viewDidLoad中)。

代码语言:javascript
复制
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos];
[self.tableView scrollToRowAtIndexPath:indexPath 
    atScrollPosition:UITableViewScrollPositionTop animated:YES];

这在iOS5模拟器中非常有效。

在iOS 6模拟器中尝试此操作时,不会执行滚动。我没有收到任何错误。我已经检查过,linePos和chapterPos接收到正确的值,但没有执行滚动。

你知道为什么吗?

EN

回答 8

Stack Overflow用户

发布于 2014-12-12 23:40:13

Objective-C

代码语言:javascript
复制
[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
        NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:3 inSection:0];
        [self.tableView scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
});

斯威夫特

代码语言:javascript
复制
tableView.reloadData()
DispatchQueue.main.async {
    let indexPath = IndexPath(row: linePos, section: chapterPos)
    self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
票数 68
EN

Stack Overflow用户

发布于 2016-01-06 01:00:09

我将这个答案添加到Fyodor Volchyok的答案中。我还发现调度解决了这个问题。我找到了一个不能调度的变通方法。

代码语言:javascript
复制
self.tableView.reloadData()
let index = // the desired index path

// For some reason, requesting the cell prior to 
// scrolling was enough to workaround the issue.
self.tableView.cellForRowAtIndexPath(index)

self.tableView.scrollToRowAtIndexPath(index, atScrollPosition: .Top, animated: false)
票数 3
EN

Stack Overflow用户

发布于 2020-01-17 23:47:18

这在iOS 12和13中有效:

代码语言:javascript
复制
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { 
    self.tableView.scrollToRow(at: IndexPath(row: 0, section: 1), at: .bottom, animated: true) 
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12514770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档