'NSRangeException':第(0)行超出边界(1)的第(0)行

时间:2017-04-28 12:46:12

标签: uitableview core-data swift3 ios10 nsfetchedresultscontroller

我有一个聊天应用程序,在我的tablview中使用NSFetchResultsController获取和更新消息。 这是我的代码:

func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>)
    {
        self.tblViewChatLog.endUpdates()
        scrollToBottom(animated: true)

        if self.fetchedResultsController.fetchedObjects?.count == 0 {
        }
        else {
        }
    }

    func scrollToBottom(animated: Bool)
    {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1)
        {
            let secCount = self.tblViewChatLog.numberOfSections
            if secCount > 0
            {
                let sections = self.fetchedResultsController.sections
                let secInfo = sections?.last

                let rows = secInfo?.objects //secInfo?.numberOfObjects
                if (rows?.count)! > 0
                {
                    let indexPath = NSIndexPath(row: (rows?.count)!-1, section: secCount-1)
                    self.tblViewChatLog.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: animated)
                    self.view.updateConstraintsIfNeeded()
                    self.tblViewChatLog.updateConstraintsIfNeeded()
                }
            }
        }
    }

我在尝试更新我的桌子时遇到以下错误。

Terminating app due to uncaught exception 'NSRangeException', reason: '-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:]: row (1) beyond bounds (1) for section (0).' *** First throw call stack: (0x21f15b0b 0x216d2dff 0x21f15a51 0x2672a9c1 0x2672a45f 0x13e7b4 0x54e90 0x19adba7 0x19b78e9 0x19adb93 0x19c156d 0x19afb43 0x19b2157 0x21ed7755 0x21ed5c4f 0x21e241c9 0x21e23fbd 0x23440af9 0x2655c435 0xcc554 0x21ad0873) libc++abi.dylib: terminating with uncaught exception of type NSException

1 个答案:

答案 0 :(得分:2)

该错误告诉您,您的表在索引1处没有任何行。这意味着您的表视图由1行组成,indexPath.row的最大范围可以为0(因为UITableView的行索引从0开始) 。每当你在索引路径上调用超出表范围的行时,就会抛出错误。