今天早上我刚安装了新的Xcode,其中包括iOS 6。
我有一个加载了plist文件的表视图,其中包含章节和行。章节定义了各个部分。
用户选择章节和行,表格视图自动滚动到正确的位置(在viewDidLoad中)。
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop animated:YES];
这在iOS5模拟器中非常有效。
在iOS 6模拟器中尝试此操作时,不会执行滚动。我没有收到任何错误。我已经检查过,linePos和chapterPos接收到正确的值,但没有执行滚动。
你知道为什么吗?
发布于 2014-12-12 23:40:13
Objective-C
[self.tableView reloadData];
dispatch_async(dispatch_get_main_queue(), ^{
NSIndexPath *rowIndexPath = [NSIndexPath indexPathForRow:3 inSection:0];
[self.tableView scrollToRowAtIndexPath:rowIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
});
斯威夫特
tableView.reloadData()
DispatchQueue.main.async {
let indexPath = IndexPath(row: linePos, section: chapterPos)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
发布于 2016-01-06 01:00:09
我将这个答案添加到Fyodor Volchyok的答案中。我还发现调度解决了这个问题。我找到了一个不能调度的变通方法。
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)
发布于 2020-01-17 23:47:18
这在iOS 12和13中有效:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.tableView.scrollToRow(at: IndexPath(row: 0, section: 1), at: .bottom, animated: true)
}
https://stackoverflow.com/questions/12514770
复制相似问题