2016-10-24 105 views
0

爲了解決此問題,我已將我的代碼簡化爲十幾行。我將fetchedResultsController鏈接到jsqMessagesViewController框架。向集合中插入一個項目重新加載集合中的每個單個項目查看

該應用程序的功能是當您按下發送按鈕時,它將一個對象插入到上下文中,該對象激活fetchedResultsController的.Insert

出於某種原因,每次我插入集合查看它重新加載集合視圖中的每一件事。這導致了一系列錯誤,而且我不知道是我做錯了什麼,如果它是JSQ框架本身,還是這是正常的?

無論如何,這裏是我的整個代碼。它並不長,如果你熟悉快速的代碼數據,這對你來說應該是一件輕而易舉的事情。

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 
} 

} 

回答

0

我不確定,但嘗試,如果這可以幫助你。

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

您好Deepak,我的問題是不是我想一次重新加載一件事。事實上,我不想重新加載任何東西。但某些原因,將一個項目插入到集合中會重新加載集合視圖中的每個其他項目。我知道的不是應該發生的事情。 – slimboy

相關問題