通过NSNotification从UICollectionView中删除单元格 [英] Deleting cells from UICollectionView via NSNotification

查看:178
本文介绍了通过NSNotification从UICollectionView中删除单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的基于UICollectionView的应用程序 - 一个UICollectionView和一个基于NSMutableArray的数据模型,为了简单起见。

I have a simple UICollectionView based app - one UICollectionView and a NSMutableArray based data model for simplicity.

我可以通过didSelectItemAtIndexPath:delegate方法删除没有问题的单元格:

I can delete cells with no problem via the didSelectItemAtIndexPath: delegate method:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [self.data removeObjectAtIndex:[indexPath row]];
    [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
}

但是,我正在尝试通过<$ c添加删除选项$ c> UIMenuController 在 UICollectionViewCell 子类中,它是通过 UILongPressGestureRecognizer 触发的很好,我成功触发 NSNotification

However, I'm trying to add a delete option via a UIMenuController in a UICollectionViewCell subclass which is triggered via a UILongPressGestureRecognizer which all works fine and I successfully trigger an NSNotification

-(void)delete:(id)sender{
      NSLog(@"Sending deleteme message");
      [[NSNotificationCenter defaultCenter] postNotificationName:@"DeleteMe!" object:self userInfo:nil];
}

我在ViewController中捕获它并调用以下方法:

I catch it in my ViewController and call the following method:

-(void)deleteCell:(NSNotification*)note{
       MyCollectionViewCell *cell = [note object];
       NSIndexPath *path = nil;
       if((path = [self.collectionView indexPathForCell:cell]) != nil){
           [self.data removeObjectAtIndex:[path row]];
           [self.collectionView deleteItemsAtIndexPaths:@[path]];
       }
}

它在deleteItemsAtIndexPaths上崩溃:调用

And it crashes on the deleteItemsAtIndexPaths: call

-[UICollectionViewUpdateItem action]: unrecognized selector sent to instance 0xee7eb10

我已经检查了所有明显的东西 - 比如来自NSNotification的对象和从indexPathForCell:call创建的indexPath,这一切看起来都很好。看起来我正在调用deleteItemsAtIndexPath:在两个地方使用相同的信息,但由于某种原因,当它通过通知路由时它会失败。

I've checked everything obvious - like the object from NSNotification and the indexPath created from the indexPathForCell: call and it all seems totally fine. It seems like I'm calling deleteItemsAtIndexPath: with the same information in both places, but for some reason it fails when it goes via the notification route.

这是信息在错误中给出的地址:

This is the info at the address given in the error:

(lldb) po 0xee7eb10
(int) $1 = 250080016 <UICollectionViewUpdateItem: 0xee7eb10> index path before update (<NSIndexPath 0x9283a20> 2 indexes [0, 0]) index path after update ((null)) action (delete)

更新后的索引路径可能很重要...

Perhaps the index path after update being null is significant...

任何想法?

推荐答案

我找到了一个粗略但有效的解决方法,它甚至会检查行动是否已在未来版本中实施(优于某个类别)

I found a crude but working workaround, and it even checks if action is already implemented in a future release (better than a category)

// Fixes the missing action method when the keyboard is visible
#import <objc/runtime.h>
#import <objc/message.h>
__attribute__((constructor)) static void PSPDFFixCollectionViewUpdateItemWhenKeyboardIsDisplayed(void) {
    @autoreleasepool {
    if ([UICollectionViewUpdateItem class] == nil) return; // pre-iOS6.
    if (![UICollectionViewUpdateItem instancesRespondToSelector:@selector(action)]) {
            IMP updateIMP = imp_implementationWithBlock(^(id _self) {});
            Method method = class_getInstanceMethod([UICollectionViewUpdateItem class], @selector(action));
            const char *encoding = method_getTypeEncoding(method);
            if (!class_addMethod([UICollectionViewUpdateItem class], @selector(action), updateIMP, encoding)) {
                NSLog(@"Failed to add action: workaround");
            }
        }
    }
}

编辑:添加了对iOS5的检查。

编辑2:我们在很多商业项目中发布了这些内容( http:// pspdfkit.com )它很棒。

Added check for iOS5.
We're shipping that in lots of commercial projects (http://pspdfkit.com) and it works great.

这篇关于通过NSNotification从UICollectionView中删除单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆