【发布时间】:2012-10-15 10:58:01
【问题描述】:
为了简单起见,我有一个基于 UICollectionView 的简单应用程序 - 一个 UICollectionView 和一个基于 NSMutableArray 的数据模型。
我可以通过 didSelectItemAtIndexPath: 委托方法毫无问题地删除单元格:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[self.data removeObjectAtIndex:[indexPath row]];
[self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
}
但是,我正在尝试通过UICollectionViewCell 子类中的UIMenuController 添加删除选项,该子类通过UILongPressGestureRecognizer 触发,一切正常,我成功触发NSNotification
-(void)delete:(id)sender{
NSLog(@"Sending deleteme message");
[[NSNotificationCenter defaultCenter] postNotificationName:@"DeleteMe!" object:self userInfo:nil];
}
我在 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]];
}
}
它在 deleteItemsAtIndexPaths 上崩溃:调用
-[UICollectionViewUpdateItem action]: unrecognized selector sent to instance 0xee7eb10
我已经检查了所有明显的东西——比如来自 NSNotification 的对象和从 indexPathForCell: 调用创建的 indexPath,这一切看起来都很好。似乎我在两个地方都使用相同的信息调用 deleteItemsAtIndexPath:,但由于某种原因,它在通过通知路由时失败了。
这是错误中给出的地址的信息:
(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)
也许更新为空后的索引路径很重要……
有什么想法吗?
【问题讨论】:
-
在
deleteCell:你使用self.collectionViewOne和self.collectionView- 这是故意的吗? -
我可以确认从通知中插入新项目时也会发生这种情况。
-
我也遇到了同样的问题,恐怕是UIMenuController引起的!当我在 UICollectionViewCell 中嵌入 UITextView 时,如果长按 UITextView 以显示系统默认菜单,则会收到警告:“[UICollectionViewUpdateItem action]: unrecognized selector sent to instance”,如果插入项目,则与您的错误相同。
-
好的,所以原因似乎是 UICollectionViewCell 内容视图中的 UITextView 本身。不知道它是否相关,但使用 UITextView 创建单元格时的第一个警告是:“设置集合视图的第一响应者视图,但我们不知道它的类型(单元格/页眉/页脚)”。随后尝试在集合视图中插入/删除项目会导致崩溃。就我而言,这只是发生在通知块内的巧合。 @melps 你的 MyCollectionViewCell 类是否包含 UITextView?
-
认为我已经确定了问题所在,在我的情况下,当键盘显示时尝试在集合视图中插入/删除项目时会发生异常。在插入/删除之前先关闭键盘似乎可以解决问题
标签: ios uicollectionview uicollectionviewcell