我正在尝试使用户能够从聊天中删除消息,我正在使用MessageKit中基于collectionView lib的部分。我试图长按删除部分。我做错了什么?
我遇到此错误
由于未捕获的异常而终止应用程序 “ NSInternalInconsistencyException”,原因:“试图删除项目0 从第1节开始,但更新前只有1个节。
长按
@objc func longPressed(sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.began {
let touchPoint = sender.location(in: self.messagesCollectionView)
if let indexPath = messagesCollectionView.indexPathForItem(at: touchPoint){
//indexPath.row = 0
//indexPath.section = 1
//row always is 0, and section +1 for every next message
messageList.remove(at: indexPath.section)
collectionView.performBatchUpdates({
collectionView.deleteItems(at: [indexPath])
collectionView.reloadData()
}, completion: nil)
}
}
}
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
return messageList.count
}
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
return messageList[indexPath.section]
}
答案 0 :(得分:0)
像这样更新您的代码:
if let indexPath = messagesCollectionView.indexPathForItem(at: touchPoint){
//indexPath.row = 0
//indexPath.section = 1
//row always is 0, and section +1 for every next message
messageList.remove(at: indexPath.section)
collectionView.reloadData()
}
从阵列中删除消息后,只需重新加载集合视图即可。
希望这会有所帮助。