gpt4 book ai didi

ios - 将一个项目插入集合中会重新加载 collectionView 中的每个项目

转载 作者:行者123 更新时间:2023-11-28 08:25:19 24 4
gpt4 key购买 nike

为了这个问题,我已经将我的代码减少到十几行。我将 fetchedResultsController 链接到 jsqMessagesViewController 框架。

应用程序所做的是当您按下发送按钮时,它会将一个对象插入到上下文中,这会激活 fetchedResultsController 的 .Insert

出于某种原因,每次我插入 Collection View 时,它都会重新加载 Collection View 中的所有内容。这导致了一大堆错误,我不知道这是我做错了什么,是 JSQ 框架本身的问题,还是这是正常的?

无论如何,这是我的全部代码。篇幅不长,如果您熟悉 swift 中的代码数据,那么阅读起来应该会轻而易举。

class ChatController: JSQMessagesViewController, NSFetchedResultsControllerDelegate {

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

lazy var fetchedResultsControler: NSFetchedResultsController<NSFetchRequestResult> = {

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Message")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: true)]
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.context, sectionNameKeyPath: nil, cacheName: nil)
frc.delegate = self
return frc
}()

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
if type == .insert {
collectionView.insertItems(at: [newIndexPath!])
scrollToBottom(animated: true)
}
}

override func viewDidLoad() {
super.viewDidLoad()
self.senderId = ""
self.senderDisplayName = ""

do {
try fetchedResultsControler.performFetch()
}

catch { }
}

override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) {
let message = NSEntityDescription.insertNewObject(forEntityName: "Message", into: context) as! Message

message.text = text
message.timestamp = Date() as NSDate?
}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageDataForItemAt indexPath: IndexPath!) -> JSQMessageData! {
let msg : Message = fetchedResultsControler.object(at: indexPath) as! Message
let messageData = JSQMessage(senderId: senderId, displayName: senderDisplayName, text: msg.text)
return messageData
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (fetchedResultsControler.sections?[0].numberOfObjects)!
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
cell.layer.shouldRasterize = true
cell.layer.rasterizationScale = UIScreen.main.scale
return cell
}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! {
let bubbleFactory = JSQMessagesBubbleImageFactory()
return bubbleFactory?.outgoingMessagesBubbleImage(with: .black)
}

override func collectionView(_ collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAt indexPath: IndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}

}

最佳答案

我不确定,但请尝试这是否可以帮助您。

collectionView?.performBatchUpdates({
self.collectionView?.reloadItems(at: indexPaths)
}) { completed in
//Your Completion Code
}
}

关于ios - 将一个项目插入集合中会重新加载 collectionView 中的每个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40211811/

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