我有一个将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)

导致撞车。我错过了什么?

最佳答案

你对这个有意见

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)

08-07 21:56