首页 > 解决方案 > 异常'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',原因:'试图滚动到无效的索引路径

我的错误在哪里以及如何解决?

标签: swiftcore-datansfetchedresultscontrollercollectionview

解决方案


推荐阅读