我有一个带有UITabBar
. 每个选项卡都是一个,里面UINavigationController
有几个UIViewControllers
。其中一个视图控制器包含一个UITableView
,我想显示一个用于删除长按的浮动菜单UITableViewCell
。
我正在使用UIMenuController
但它没有显示,因为单元格拒绝成为第一响应者。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0; //second
[table addGestureRecognizer:lpgr];
[lpgr release];
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
CGPoint p = [gestureRecognizer locationInView: table];
NSIndexPath *indexPath = [table indexPathForRowAtPoint:p];
if (indexPath != nil) {
UITableViewCell* cell = [self tableView:table cellForRowAtIndexPath: indexPath];
[cell becomeFirstResponder];
UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(delete:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:delete, nil]];
[menu setTargetRect:cell.frame inView:cell.superview];
[menu setMenuVisible:YES animated:YES];
}
}
}
在 UITableViewCell 我已经覆盖了方法:
-(BOOL) canBecomeFirstResponder {
return YES;
}
关于为什么细胞没有成为第一响应者的任何想法?
谢谢!