ios - NSInternalInconsistencyException',原因 : 'Invalid update: invalid number of items in section 1 - While having multiple Sections

标签 ios swift uicollectionview

当我尝试插入删除单元格(有 2 个补充标题部分)时,使用带有 FlowLayout 的 swift UICollectionView 时,它会给出以下错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 1 节中的项目数无效。更新 (1) 后现有部分中包含的项目数必须等于项目数更新前包含在该部分中的 (0),加上或减去从该部分插入或删除的项目数(0 插入,0 删除)以及加上或减去移入或移出该部分的项目数(0 移入, 0 移出)。'

  1. 最初我的 UIcollection View 只显示两个部分。
  2. 然后根据用户操作,我在第 0 部分或第 1 部分下添加项目,这取决于项目类别。

每当我将部分设为 1 时,insertItemsAtIndexPaths 和 deleteItemsAtIndexPaths 都工作正常。我正在使用 IOS9 和 swift 。看过很多帖子并尝试过很多东西,但一直被困在这里。 var selectedPlayerDispCollection = [(String, PlayersProfile)]

func numberOfSectionsInCollectionView(collectionView:UICollectionView!) -> Int {
        return 2
}

 func collectionView(collectionView: UICollectionView!,numberOfItemsInSection section: Int) -> Int {
        return selectedPlayerDispCollection.count
}

func performBatchUpdates(updates: (() -> Void)?,
    completion: ((Bool) -> Void)?){
        self.collectionView.reloadData()
}

**// For now adding in section 0 , depending on condition will add in section 0 or section 1
    func addPlayerView(playertapped : PlayersProfile) {
    let count = selectedPlayerDispCollection.count
    //let index = count > 0 ? count - 1 : count
    let index = count
    let playerDisSec = [(playertapped.playerSpeciality, playertapped)]
    selectedPlayerDispCollection.insert(playerDisSec, atIndex: index)
    let indexPath = NSIndexPath(forRow: index, inSection: 0)
    collectionView.insertItemsAtIndexPaths([indexPath])
}**


/*func addPlayerView(playertapped : PlayersProfile) {
     let count = selectedPlayerDispCollection.count
      let index = count > 0 ? count - 1 : count
        selectedPlayerDispCollection.insert(playertapped, atIndex: index)
        let indexPath = NSIndexPath(forRow: index, inSection: 0)
        collectionView.insertItemsAtIndexPaths([indexPath])
}*/

最佳答案

正如我的评论所说:问题是您使用一个数组来确定两个部分中的项目数。因此,从数组中删除一项会导致 Collection View “丢失”两个单元格。丢失两个单元格并不符合您告诉 collectionView 的内容:删除 indexPath abc 处的一个单元格。

解决方案一

使用两个单独的数组作为每个部分的支持,或使用一个包含子数组的数组。

方案二

告诉 collectionview 删除/添加两个项目:

if let index = selectedPlayerDispCollection.indexOf(playertapped){
    let indexPath0 = NSIndexPath(forItem: index, inSection: 0)
    let indexPath1 = NSIndexPath(forItem: index, inSection: 1)
    selectedPlayerDispCollection.removeAtIndex(index)
    collectionView.deleteItemsAtIndexPaths([indexPath0, indexPath1])
}

添加新项目时必须使用相同的逻辑。

关于ios - NSInternalInconsistencyException',原因 : 'Invalid update: invalid number of items in section 1 - While having multiple Sections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34567221/

相关文章:

swift - 带导航栏的 UICollectionView 顶部的临时额外空间

ios - 如何使 UICollectionView 通过自动调整大小来填充其 super View ?

objective-c - iOS5下添加SplitViewController View 麻烦

ios - iOS 7 中导航栏的默认背景颜色是什么?

android - 带触发器的平台

ios - AVPlayer 不会在 iOS9 中播放来自 url 的视频

json - 如何使用按钮或拉动刷新来刷新 TableView 和 JSON 数据?

ios - UISwipeGestureRecognizer 抛出无法识别的选择器发送错误

ios - UICollectionView 自定义布局标题 View

ios - NSInternalInconsistencyException - “请求 MSCollectionElementKindCurrentTimeHorizo​​ntalGridline 装饰 View 的布局属性