2012-10-15 9 views
8

Tôi có một ứng dụng dựa trên UICollectionView đơn giản - một UICollectionView và một mô hình dữ liệu dựa trên NSMutableArray để đơn giản.Xóa các ô khỏi UICollectionView qua NSNotification

tôi có thể xóa các tế bào không có vấn đề thông qua didSelectItemAtIndexPath: Phương pháp đại biểu:

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

Tuy nhiên, tôi đang cố gắng để thêm một tùy chọn xóa thông qua một UIMenuController trong một lớp con UICollectionViewCell mà được kích hoạt thông qua một UILongPressGestureRecognizer mà tất cả hoạt động tốt và tôi thành công kích hoạt NSNotification

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

tôi bắt nó trong ViewController của tôi và gọi phương thức sau đây:

-(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]]; 
     } 
} 

Và nó bị treo trên deleteItemsAtIndexPaths: gọi

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

Tôi đã kiểm tra tất cả mọi thứ rõ ràng - như các đối tượng từ NSNotification và indexPath tạo từ indexPathForCell: gọi và tất cả dường như hoàn toàn tốt. Có vẻ như tôi đang gọi deleteItemsAtIndexPath: với cùng một thông tin ở cả hai nơi, nhưng vì một số lý do nó không thành công khi nó đi qua tuyến thông báo.

Đây là thông tin tại địa chỉ được đưa ra trong các lỗi:

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

Có lẽ con đường chỉ số sau khi cập nhật là null là đáng kể ...

Bất kỳ ý tưởng?

+0

trong 'deleteCell:' bạn sử dụng 'self.collectionViewOne' và 'self.collectionView' - đó là mục đích? –

+0

xin lỗi - đó là lỗi đánh máy. – melps

+0

Tôi có thể xác nhận điều này cũng xảy ra khi chèn các mục mới từ một thông báo. – Brett

Trả lời

25

Tôi tìm thấy một workaround thô nhưng làm việc, và nó thậm chí còn kiểm tra xem hành động đã được thực hiện trong một thông cáo trong tương lai (tốt hơn so với một thể loại)

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

Chỉnh sửa: Thêm kiểm tra cho iOS5.
Chỉnh sửa2: Chúng tôi đang giao hàng trong nhiều dự án thương mại (http://pspdfkit.com) và hoạt động tốt.

+1

Câu trả lời này làm việc như một sự quyến rũ đối với tôi. Nên được đánh dấu là giải pháp. –

+0

thx, đã làm việc cho tôi. BTW, tôi đã nhận được sau đó khi thiết lập một cái nhìn bên trong một tế bào như là một responder đầu tiên, và không có bàn phím có thể nhìn thấy –

+0

Có ai có một ví dụ tối thiểu về những gì gây ra điều này? Tôi đang gặp sự cố trong một ứng dụng khác mà không cần bàn phím được hiển thị hoặc thay đổi thiết bị trả lời đầu tiên. –

0

Bạn có thể chuyển vào số NSIndexPath của ô đã chọn trong từ điển userInfo của thông báo của bạn. Bạn có thể đặt số đó trong tag của ô tùy chỉnh của mình khi được tạo.

+0

Nhưng nó không phải là một vấn đề của NSIndexPath là sai, vì vậy tôi không chắc chắn những gì sẽ giúp đỡ. – melps

0

Tôi gặp vấn đề tương tự. Ứng dụng của tôi sẽ gặp sự cố khi tôi cố chèn ô vào số UICollectionView sau khi tôi đã chọn văn bản trong một số UITextField được đặt bên trong collectionViewCell. Sửa chữa của tôi là từ chức người trả lời đầu tiên trước khi việc chèn xảy ra. Kể từ khi tôi đã sử dụng một NSFetchedResultsController để xử lý các cập nhật Tôi đặt này vào đầu controllerWillChangeContent:

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

tôi thấy findAndResignFirstResponderfrom this SO answer