gpt4 book ai didi

swift - UICollectionViewController : Terminating app due to uncaught exception 'NSInternalInconsistencyException'

转载 作者:行者123 更新时间:2023-11-28 06:40:14 25 4
gpt4 key购买 nike

原因:'试图将项目 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 通过调用返回 1numberOfSectionsInCollectionView 检查数据的一致性时,它会注意到不一致。

将要插入的节号更改为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com