这里有一个奇怪的问题。我想将表格 View 滚动到第 1 部分第 0 行的底部。我正在使用此代码:
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 1), atScrollPosition: .Bottom, animated: true)
它似乎在 iOS 9.0 及更高版本的模拟器中完美运行。然而,在我的设备(iOS 8.1)和 8.3 的模拟器上,它的作用完全相反。它滚动到单元格的顶部而不是底部!似乎很奇怪。任何想法我可能在这里做错了什么?非常感谢任何指点!谢谢
最佳答案
根据 this answer对于类似的问题,它似乎是 iOS 8 中的一个错误。解决方法是将滚动延迟一个非常小的量(此代码也可以在链接的答案中找到,但它在 Objective-C 中)。请注意,我没有测试此代码,因为我没有安装 iOS 8 模拟器。
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 1), atScrollPosition: .Bottom, animated: true)
}
编辑:这是 Swift 3 中的答案:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.tableView.scrollToRow(at: IndexPath(row: 0, section: 1), at: .bottom, animated: true)
}
关于ios - scrollToRowAtIndexPath 在 iOS 8 上无法正确滚动到底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38611617/