Deleting cells from UICollectionView via NSNotification

北城以北 提交于 2019-11-30 09:20:28

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

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

You could pass in the NSIndexPath of the selected cell in the userInfo dictionary of your notification. You could set that in the tag of your custom cell when it is created.

moshe

I had the same issue. My app would crash when I tried to insert a cell into my UICollectionView after I had selected text in a UITextField which was placed inside a collectionViewCell. My fix was to resign the first responder before the insertion happened. Since I was using a NSFetchedResultsController to handle the updating I put this at the beginning of controllerWillChangeContent:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [[UIApplication sharedApplication].keyWindow findAndResignFirstResponder];
    ...
}

I found findAndResignFirstResponder from this SO answer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!