Uitableview ';NSRangeException';:第(1)行超出第(0)节的边界(1)
我有一个聊天应用程序,在该应用程序中,使用NSFetchResultsController在我的tablview中获取和更新消息。 这是我的密码:Uitableview ';NSRangeException';:第(1)行超出第(0)节的边界(1),uitableview,core-data,swift3,ios10,nsfetchedresultscontroller,Uitableview,Core Data,Swift3,Ios10,Nsfetchedresultscontroller,我有一个聊天应用程序,在该应用程序中,使用NSFetchResultsController在我的tablview中获取和更新消息。 这是我的密码: func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { self.tblViewChatLog.endUpdates() scrollToBottom(anim
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()
}
}
}
}
func controllerDidChangeContent(\ucontroller:NSFetchedResultsController)
{
self.tblViewChatLog.endUpdates()
scrollToBottom(动画:true)
如果self.fetchedResultsController.fetchedObject?.count==0{
}
否则{
}
}
func scrollToBottom(动画:Bool)
{
DispatchQueue.main.asyncAfter(截止日期:.now()+0.1)
{
设secCount=self.tblViewChatLog.numberOfSections
如果secCount>0
{
让sections=self.fetchedResultsController.sections
设secInfo=节?最后
设行=secInfo?.objects//secInfo?.numberOfObjects
如果(行数?.count)!>0
{
设indexPath=NSIndexPath(行:(行?.count)!-1,节:secCount-1)
self.tblViewChatLog.scrollToRow(at:indepath作为indepath,at:.底部,动画:动画)
self.view.updateConstraintSifRequired()
self.tblViewChatLog.updateConstraintSifRequired()文件
}
}
}
}
尝试更新表时出现以下错误
由于未捕获的异常“NSRangeException”而终止应用程序,原因:'-[UITableView\u contentOffsetForScrollingToRowatineXPath:atScrollPosition:]:第(1)行超出了节(0)的边界(1)。'
***第一次抛出调用堆栈:
(0x21f15b0b 0x216d2dff 0x21f15a51 0x2672a9c1 0x2672a45f 0x13e7b4 0x54e90 0x19b78e9 0x19adb93 0x19c156d 0x19afb43 0x19b2157 0x21ed7755 0x21ed5c4f 0x21e241c9 0x21e23fbd 0x23440af9 0x2655c435 0xcc554 0x21ad0873)
libc++abi.dylib:以NSException类型的未捕获异常终止
该错误告诉您表在索引1处没有任何行。这意味着表视图由1行组成,indexath.row的最大范围可以是0(因为UITableView的行索引从0开始)。无论何时在索引路径调用超出表范围的行,它都会抛出一个错误