将一个项目插入到集合中会重新加载collectionView

时间:2016-10-24 06:19:20

标签: ios swift core-data jsqmessagesviewcontroller

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

当你按下发送按钮时,应用程序会做什么,它会将一个对象插入到上下文中,从而激活fetchedResultsController的.Insert

出于某种原因,每次我插入集合View时,它都会重新加载集合视图中的每一件事。这导致了TON的错误,我无法判断它是否是我做错的事情,如果它是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
}

}

1 个答案:

答案 0 :(得分:0)

我不确定,但试试这可以帮助你。

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