【发布时间】:2014-01-09 23:19:39
【问题描述】:
我正在尝试使用 UIMenuController 对表格单元格执行自定义操作,UIMenuController 由长按触发。
我在 UITableViewController 子类的 viewDidLoad 方法中注册了 UILongPressGestureRecognizer,并使用 @selector(handleMyAction) 添加了自定义项。
- (void)viewDidLoad
{
[super viewDidLoad];
[self.refreshControl addTarget:self action:@selector(refreshView:) forControlEvents:UIControlEventValueChanged];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPressGesture.minimumPressDuration = .5;
longPressGesture.delegate = self;
[self.tableView addGestureRecognizer:longPressGesture];
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
CGPoint point = [gestureRecognizer locationInView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];
if(indexPath == nil) return ;
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIMenuItem *it = [[UIMenuItem alloc] initWithTitle:@"My Action on this cell" action:@selector(handleMyAction:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:it, nil]];
[menu setTargetRect:cell.frame inView:cell.superview];
[menu setMenuVisible:YES animated:YES];
[self becomeFirstResponder];
}
}
我也覆盖了
- (BOOL)canBecomeFirstResponder{
return YES;
}
当我按下一个单元格时,带有自定义条目的上下文菜单会正确显示。但是,问题是如何实现处理自定义操作的方法,这应该在点击的单元格上执行。
- (void)handleMyAction:(id)sender
{
NSLog(@"Action triggered, however need some way to refer the tapped cell");
}
因为我可以在此方法中获得的唯一信息是发送者,即 UIMenuController 自身,但我不知道如何获取触发菜单的单元格,因此我可以对单元格本身执行进一步操作.
有人可以帮我解决这个问题吗?
谢谢。 海
【问题讨论】:
标签: ios ios7 uitableview uimenucontroller