swift - 异常'NSInvalidArgumentException',原因:'试图滚动到无效的索引路径
问题描述
我创建了基于核心数据的消息传递应用程序。聊天控制器发生了一次我无法修复的崩溃。当它发生时我找不到模式,它似乎是随机出现的。它发生在以下方法中:
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',原因:'试图滚动到无效的索引路径
我的错误在哪里以及如何解决?
解决方案
推荐阅读
- excel - 循环浏览网站中检查汽车注册的单元格列,然后在将日期放入下一列后移至下一个
- reactjs - 是否有一个反应签名板允许输入字符或绘制签名
- javascript - 类中返回相同值的两个函数有什么区别
- wget - wget 看不到/下载某些文件?
- r - 通过保留另一列值作为参考来删除不唯一的行
- haskell - 解析 `f = f (<*>) pure` 的类型
- java - 如何使用 ProcessBuilder 将值从 Python 脚本返回到 Java?
- php - 如何从 PHP 联系表单打印电子邮件中的下拉值
- sql - 已请求 SQL 调优建议
- excel - 非常绿色的 VBA 用户,第 2 行以下的所有内容都可以正常工作,但我如何使它上面的工作正常?