Deleting cells from UICollectionView via NSNotification

后端 未结 3 1512
栀梦
栀梦 2020-12-31 17:11

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

I can delete cells with no problem via the di

相关标签:
3条回答
  • 2020-12-31 17:57

    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.

    0 讨论(0)
  • 2020-12-31 17:58

    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.

    0 讨论(0)
  • 2020-12-31 18:11

    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

    0 讨论(0)
提交回复
热议问题