- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试插入删除单元格(有 2 个补充标题部分)时,使用带有 FlowLayout 的 swift UICollectionView 时,它会给出以下错误:
由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 1 节中的项目数无效。更新 (1) 后现有部分中包含的项目数必须等于项目数更新前包含在该部分中的 (0),加上或减去从该部分插入或删除的项目数(0 插入,0 删除)以及加上或减去移入或移出该部分的项目数(0 移入, 0 移出)。'
每当我将部分设为 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/
我是一名优秀的程序员,十分优秀!