1

How to handle delete action in JSQMessageController, I have implemented method

override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) {

     self.collectionView?.deleteItemsAtIndexPaths([indexPath])

}

Also override method

override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
   // Do the custom JSQM stuff
    super.collectionView(collectionView, shouldShowMenuForItemAtIndexPath: indexPath)
  // And return true for all message types (we don't want the long press menu disabled for any message types)
  return true
}

override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
   super.collectionView(collectionView, canPerformAction: action, forItemAtIndexPath: indexPath, withSender: sender)
   return true
}

override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
  super.collectionView(collectionView, performAction: action, forItemAtIndexPath: indexPath, withSender: sender)
}

but I get crash on menu item what else to be done to work, any help would be appreciated, thanks in advance.

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
iOS_Raj
  • 200
  • 1
  • 1
  • 10
  • what does the crash say / – Teja Nandamuri Apr 05 '16 at 12:51
  • Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (8) must be equal to the number of items contained in that section before the update (8), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).' – iOS_Raj Apr 05 '16 at 12:53
  • Hello dear this not a proper solution for reload data in collection. its cause issue when we scrolling fast or index path doesn't exist try below solution – Hitesh Surani Apr 05 '16 at 13:06

3 Answers3

0
  1. Delete data from the array or dictionary at specific index path.

  2. Reload collection view data.

override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) 
{

     //Code for delete data from array or dictionary
     [JSQMessagesCollectionView reloaddata];

}
mfitzp
  • 15,275
  • 7
  • 50
  • 70
Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
0

There is good workaround to reloadData but issue is something else .....

It is saying that you changed dataSource array and changes are not done in collectionView before deleteItemsAtIndexPaths called.

dev_m
  • 796
  • 6
  • 12
0

Look at the source code from JSQMessagesViewController.m:

- (void)collectionView:(JSQMessagesCollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    if (action == @selector(copy:)) {
        id<JSQMessageData> messageData = [collectionView.dataSource collectionView:collectionView messageDataForItemAtIndexPath:indexPath];
        [[UIPasteboard generalPasteboard] setString:[messageData text]];
    }
    else if (action == @selector(delete:)) {
        [collectionView.dataSource collectionView:collectionView didDeleteMessageAtIndexPath:indexPath];

        [collectionView deleteItemsAtIndexPaths:@[indexPath]];
        [collectionView.collectionViewLayout invalidateLayout];
    }
}

so You shouldn't call

self.collectionView?.deleteItemsAtIndexPaths([indexPath])

from

override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!)

You only should delete message from datasource at this method.