原因:'试图将项目 0 插入第 1 部分,但更新后只有 1 个部分'
我不明白这是什么问题。如果数组被插入第 1 节并且(只有)1 个节那么为什么不能插入?
var ref: FIRDatabaseReference!
var messages: [FIRDataSnapshot]! = []
private var refHandle: FIRDatabaseHandle!
override func viewDidLoad() {
super.viewDidLoad()
// Register cell classes
collectionView?.registerClass(ChatLogCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView?.backgroundColor = UIColor.blueColor()
}
override func viewWillAppear(animated: Bool) {
self.messages.removeAll()
collectionView?.reloadData()
refHandle = self.ref.child("messages").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
self.messages.append(snapshot)
print(self.messages.count) //temp
self.collectionView?.insertItemsAtIndexPaths([NSIndexPath(forRow: self.messages.count - 1, inSection: 1)])
})
}
单元格代码:
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.messages.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ChatLogCollectionCell
}
最佳答案
您正在向 1
部分插入内容,这意味着必须有 2
部分,因为编号从 0
开始。当 iOS 通过调用返回 1
的 numberOfSectionsInCollectionView
检查数据的一致性时,它会注意到不一致。
将要插入的节号更改为 0
。
self.collectionView?.insertItemsAtIndexPaths([NSIndexPath(forItem: self.messages.count - 1, inSection: 0)])
关于swift - UICollectionViewController : Terminating app due to uncaught exception 'NSInternalInconsistencyException' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38164911/