当用户选择表格中的任何行时,我试图显示UIMenuController
。我正在使用UITableViewController
来显示包含自定义单元格的表格。
我的代码: -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// [tableView deselectRowAtIndexPath:indexPath animated:NO];
MyCell *cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath];
CGRect cellFrame = cell.frame;
[self.view becomeFirstResponder];
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Item1" action:@selector(action1:)];
UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Item2" action:@selector(action2:)];
UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Item3" action:@selector(action3:)];
UIMenuController * menuController = [UIMenuController sharedMenuController];
menuController.menuItems = [NSArray arrayWithObjects:menuItem, menuItem1, menuItem2, nil];
menuController.arrowDirection = UIMenuControllerArrowDown;
[menuController setTargetRect:cellFrame inView:self.view];
[menuController setMenuVisible:YES animated:YES];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
但是UIMenuController
没有显示出来。上面的代码有什么不对?
另外,我已提到these links。但没有运气。
答案 0 :(得分:0)
如果您只是在长按后才能显示菜单,则无需使用tableView:didSelectRowAtIndexPath:
并自行显示菜单。
相反,您可以使用此委托方法:
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
要隐藏标准项目(剪切,复制和粘贴),请在此处返回NO:
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}
然后你需要return YES
来自canBecomeFirstResponder
,并且出于某种原因,我也必须实施此方法:
- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return YES;
}