移动UICollectionViewCell会导致invalidateItemsAtIndexPaths错误

时间:2018-12-20 21:19:30

标签: ios swift uicollectionview uicollectionviewcell

当拖动以移动UICollectionView中的单元格时,由于项目被标记为无效,但在索引路径中丢失,因此该应用程序崩溃了。

代码成功确认执行了集合中的移动。然后,该集合将返回预期的节数和该节的单元格。在看似没有问题的情况下完成之后,发生以下错误:

  

2018-12-20 15:39:54.216391-0500 TestApp [1748:485235] ***-[UICollectionViewData invalidateItemsAtIndexPaths:],/ BuildRoot / Library / Caches / com.apple.xbs / Sources / UIKitCore中的声明失败/UIKit-3698.93.8/UICollectionViewData.m:166

     

2018-12-20 15:39:54.216878-0500 TestApp [1748:485235] ***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'试图使无效索引处的项目无效路径:{length = 2,路径= 0-1} globalIndex:1 numItems:0'

代码本身并没有做任何非常有趣的事情。确保不移动处于编辑模式的最后一个单元格(添加按钮),然后更新模型的数据。

似乎该集合中的任何项目都不应该首先失效。集合中的项目数量很小,因此整个屏幕上所有内容都在屏幕上显示,因此在这种情况下不会从视图中删除任何单元格。没有任何单元格的内容被更改,因此我不完全确定可能被标记为无效的内容,但是即使发生这种情况,我也不知道为什么会丢失它。

以下代码:

// MARK: UICollectionViewDataSource Functions

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let friendsCount = AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count
    if friendsCount >= 0 {
        return isEditingEnabled ? friendsCount + 1 : friendsCount
    } else {
        return 0
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if isEditingEnabled && indexPath.row == AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count {

        if !isUpgraded {
            var currentVideoCount = 0
            for i in 0..<AppDataModel.sharedInstance.groupModels.count {
                currentVideoCount += AppDataModel.sharedInstance.groupModels[i].friends.count
            }
            if currentVideoCount >= maxFreeVideos {
                print("Max Videos for trial reached.")
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "upgradeCell", for: indexPath)
                return cell
            }
        }

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "addFriendCell", for: indexPath)
        return cell
    }
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "friendCell", for: indexPath) as! MainViewFriendCollectionViewCell
    let friendForCell = AppDataModel.sharedInstance.groupModels[groupIndex!].friends[(indexPath as NSIndexPath).row]
    cell.setup(friendForCell, shouldHideDeleteButton: !isEditingEnabled, onDeleteFriend: self.onDeleteFriend, onForceUpload: self.onForceUpload)
    return cell
}

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    // The user cannot reorder the add button
    let addButtonIndex = AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count
    let destinationIndex = (destinationIndexPath.item >= addButtonIndex) ? addButtonIndex - 1 : (destinationIndexPath as NSIndexPath).item
    // reflect the changes to the view in the model
    AppDataModel.sharedInstance.groupModels[groupIndex!].updateFriendOrdersAfterReorder((sourceIndexPath as NSIndexPath).item, toIndex: destinationIndex)
}

func collectionView(_ collectionView: UICollectionView, targetIndexPathForMoveFromItemAt originalIndexPath: IndexPath, toProposedIndexPath proposedIndexPath: IndexPath) -> IndexPath {
    let addButtonIndex = AppDataModel.sharedInstance.groupModels[groupIndex!].friends.count
    return proposedIndexPath.item >= addButtonIndex ?  originalIndexPath : proposedIndexPath
}

0 个答案:

没有答案