首页 > 解决方案 > 'NSInternalInconsistencyException',原因:'请求节 (0) 的行数超出范围。

问题描述

当我尝试在 iOS 13 上运行我的应用程序时出现此错误,旧版本运行良好。

'NSInternalInconsistencyException', reason: 'Requested the number of rows for section (0) which is out of bounds.'

这就是我认为导致异常的原因

override func reloadData() {
    super.reloadData()

    let rows = self.numberOfRows(inSection: 0) // what I know is that this line is causing the exception
    if (rows > 0) {
        if placeholderStackView != nil {
            self.placeholderStackView.removeFromSuperview()
        }
    } else {
        setTableStatus(type: .empty)
    }

}

当我将变量行设置为它无一例外地加载的数字时,我认为 UITableView SDK 的更新导致了它,我尝试在谷歌搜索一些见解,但没有成功。

标签: iosswiftuitableview

解决方案


先检查一下self.numberOfSections。如果没有部分,则该部分中不能有行(超出范围)。

override func reloadData() {
    super.reloadData()

    guard 0 < self.numberOfSections && 0 < self.numberOfRows(inSection: 0) else {
        setTableStatus(type: .empty)
        return
    }

    if placeholderStackView != nil {
        self.placeholderStackView.removeFromSuperview()
    }
}

推荐阅读