我有一个将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)
导致崩溃。我在这里缺少什么?
答案 0 :(得分:1)
你有这个问题
let indexPath = IndexPath(row: self.tview.numberOfRows(inSection: 0), section: self.tview.numberOfSections)
尝试做-1
let indexPath = IndexPath(row: self.tview.numberOfRows(inSection: 0)-1, section: self.tview.numberOfSections-1)