使用JSQMessageViewController删除消息

时间:2016-04-05 12:46:26

标签: ios swift jsqmessagesviewcontroller

如何在JSQMessageController中处理删除操作,我已经实现了方法

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

     self.collectionView?.deleteItemsAtIndexPaths([indexPath])

}

还覆盖方法

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)
}

但是我在菜单项上崩溃还有什么工作要做,任何帮助都会受到赞赏,提前谢谢。

3 个答案:

答案 0 :(得分:0)

  1. 从特定索引路径的数组或字典中删除数据。

  2. 重新加载集合视图数据。

  3. override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) 
    {
    
         //Code for delete data from array or dictionary
         [JSQMessagesCollectionView reloaddata];
    
    }
    

答案 1 :(得分:0)

reloadData有一个很好的解决方法,但问题是其他的......

有人说你改变了dataSource数组,并且在调用deleteItemsAtIndexPaths之前没有在collectionView中完成更改。

答案 2 :(得分:0)

查看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];
    }
}

所以你不应该打电话

self.collectionView?.deleteItemsAtIndexPaths([indexPath])

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

您只应该使用此方法从数据源中删除消息。