添加标题视图后,滚动到UITableView的底部导致NSException

时间:2017-02-08 22:17:00

标签: ios swift uitableview swift3 nsindexpath

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

导致崩溃。我在这里缺少什么?

1 个答案:

答案 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)