【发布时间】:2017-02-08 22:16:59
【问题描述】:
我有一个方法可以将我的UITableView 滚动到底部:
func tableViewScrollToBottom(_ animated: Bool) {
let delay = 0.1 * Double(NSEC_PER_SEC)
let time = DispatchTime.now() + Double(Int64(delay)) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: time, execute: {
print(self.tview.numberOfRows(inSection: 0))
print(self.tview.numberOfSections)
let indexPath = IndexPath(row: self.tview.numberOfRows(inSection: 0), section: self.tview.numberOfSections)
print(indexPath)
print("it will crash now")
self.tview.scrollToRow(at: indexPath, at: .bottom, animated: animated)
})
}
它之前运行良好,但是当我添加标题视图时 - 它崩溃了。
我在控制台中看到:
8
1
[1, 8]
it will crash now
所以我不明白为什么这行:
self.tview.scrollToRow(at: indexPath, at: .bottom, animated: animated)
导致崩溃。我在这里错过了什么?
【问题讨论】:
-
如果您的部分中有 8 行,则最后一行为第 7 行。同样,如果有 1 个部分,则最后一个部分为第 0 部分。您需要从行和部分中减去 1您尝试滚动到的索引路径。
-
@dan 你当然是对的。麻烦将其添加为答案?
标签: ios swift uitableview swift3 nsindexpath