我基于核心数据创建了消息传递应用程序。聊天控制器中有一个崩溃,我无法解决。当它发生时我找不到模式,它似乎随机出现。它发生在以下方法中:
func scrollCollectionToBottom(animated: Bool){
if self.fetchedResultsControler.sections?.count == 0 {
return
}
if self.fetchedResultsControler.sections![self.fetchedResultsControler.sections!.count - 1].numberOfObjects == 0{
return
}
let lastSection = self.fetchedResultsControler.sections!.count - 1
let lastItem = self.fetchedResultsControler.sections![lastSection].numberOfObjects - 1
let indexPath = IndexPath.init(item: lastItem, section: lastSection)
if animated{
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.collectionViewMessages.scrollToItem(at: indexPath, at: .bottom, animated: true)
})
return
}
if group.type == GroupType.privat.rawValue{
constrHeightCollViewSubj.constant = 0
self.view.layoutIfNeeded()
}
DispatchQueue.main.async(){
self.collectionViewMessages.reloadData()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.collectionViewMessages.scrollToItem(at: indexPath, at: .bottom, animated: false)
})
}
}
正如我在某些情况下所说的,我遇到了这个例外的崩溃: 异常'NSInvalidArgumentException',原因:'尝试滚动到无效索引路径
我的错误在哪里以及如何解决?