在UITableViewCell中的UIView上显示longpress上的菜单

时间:2018-06-01 20:40:05

标签: ios uitableview uitapgesturerecognizer uimenuitem

在IOS应用程序中,我想在长按UITableViewCell中的UIView时显示UIMenuController。

list1 = ['item 1.1 is a sentence that drags on and continues and talks about this or that or something else but at this point it is off the side of the page', 'item 1.2', 'item 1.3',]

调用-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { myTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell==nil) { cell=(myTableViewCell*)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; } myView=<initMyView> [cell.contentView addSubview:myView]; UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCapturedOnView:)]; longTap.minimumPressDuration=0.5f; [myView addGestureRecognizer:longTap]; return cell; } - (void)longTapGestureCapturedOnView:(UITapGestureRecognizer *)gesture { items=[[NSMutableArray alloc]init]; if (gesture.state == UIGestureRecognizerStateBegan) { [items addObject:[[UIMenuItem alloc] initWithTitle:@"Copy" action:@selector(myCopy:)]]; [items addObject:[[UIMenuItem alloc] initWithTitle:@"Custom" action:@selector(myCustom:)]]; } [[UIMenuController sharedMenuController] setMenuItems:items]; [[UIMenuController sharedMenuController] update]; [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES]; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { BOOL result = NO; if(@selector(myCopy:) == action ||@selector(myCustom:) == action ) { result = YES; } return result; } ,以及在{2}中返回YES的longTapGestureCapturedOnView,但屏幕上不显示任何菜单项。我怎么可能做错了?

1 个答案:

答案 0 :(得分:0)

您还需要

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
   return YES ;
}

//

cellForRowAt中添加手势是不对的,因为它在滚动和调用时被调用。 cellReusing所以在awakeFormNib自定义类

UITableViewCell方法中添加它