小编mel*_*lps的帖子

通过NSNotification从UICollectionView中删除单元格

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

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

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

但是,我正在尝试通过一个子类添加一个删除选项UIMenuController,该UICollectionViewCell子类通过一个UILongPressGestureRecognizer正常触发并且我成功触发一个NSNotification

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

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

-(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]];
       }
}
Run Code Online (Sandbox Code Playgroud)

它在deleteItemsAtIndexPaths:call上崩溃了

-[UICollectionViewUpdateItem action]: unrecognized selector sent to instance 0xee7eb10
Run Code Online (Sandbox Code Playgroud)

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

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

(lldb) po 0xee7eb10
(int) $1 = 250080016 <UICollectionViewUpdateItem: 0xee7eb10> index path …
Run Code Online (Sandbox Code Playgroud)

ios uicollectionview uicollectionviewcell

8
推荐指数
1
解决办法
7335
查看次数