今天早上我刚刚安装了包含iOS 6的新Xcode.
我有一个表视图加载了包含章节和行的plist文件.章节定义了各个部分.
用户选择章节和行,并且tableview会自动滚动到正确的位置(在viewDidLoad中).
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop animated:YES];
Run Code Online (Sandbox Code Playgroud)
这在iOS5模拟器中效果很好.
在iOS 6模拟器中尝试此操作不会执行滚动.我没有错.我已经检查过,linePos和chapterPos接收到正确的值但是没有执行滚动.
有什么想法吗?
Fyo*_*yok 58
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];
});
Run Code Online (Sandbox Code Playgroud)
迅速
tableView.reloadData()
DispatchQueue.main.async {
let indexPath = IndexPath(row: linePos, section: chapterPos)
self.tableView.scrollToRow(at: indexPath, at: .top, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
HpT*_*erm 27
对于最新版本的iOS,请阅读Fyodor Volchyok的答案.请注意,它没有被标记为已接受的答案,因为在第一次提出问题时(2012年9月),当前的答案是工作解决方案.更新版本的iOS也遇到了同样的问题,现在由Fyodor Volchyok的答案解决了,所以你应该在那一刻给他答案.
我找到了答案.我必须先在tableview中重新加载数据
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:linePos inSection:chapterPos];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionTop animated:YES];
Run Code Online (Sandbox Code Playgroud)
即使我找到答案,我也不知道为什么它在iOS5中工作而不在iOS6中工作.
编辑
也许我应该补充一点,即使它正在工作,我仍然在显示最后一行时遇到问题,并为此发布了一个问题
UItableview scrollToRowAtIndexPath未正确显示最后一行
正如@Raj也要求它,我应该说我在触发它viewDidLoad.为了纠正最后一行没有正确显示的问题,我不得不把它放在viewDidAppear.
| 归档时间: |
|
| 查看次数: |
25912 次 |
| 最近记录: |