【问题标题】:'NSRangeException': row (1) beyond bounds (1) for section (0)'NSRangeException':第 (0) 行的第 (1) 行超出界限 (1)
【发布时间】:2017-09-26 14:17:48
【问题描述】:

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

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

【问题讨论】:

    标签: uitableview core-data swift3 ios10 nsfetchedresultscontroller


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 2019-07-04
      • 1970-01-01
      • 2016-03-03
      • 2013-05-10
      • 2019-02-25
      相关资源
      最近更新 更多