UIMenuController On UITableView Does Not Show Up In Spite Of All Methods

对着背影说爱祢 提交于 2019-12-11 06:55:51

问题


I have UITableView to display some list. Implemented UILongPressGestureRecognizer to get calls and on this I want to display menu for delete, upload, etc actions.

Following is implementation

// Registering for long press event

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self
                                          action:@selector(handleLongPress:)];
lpgr.delegate = self;
[self.myTable addGestureRecognizer:lpgr];

On Long Press My control comes to function

- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
    {
        CGPoint p = [gestureRecognizer locationInView:self.playbackTable];
        NSIndexPath *indexPath = [self.playbackTable indexPathForRowAtPoint:p];
        if (indexPath == nil)
        {
            NSLog(@"long press on table view but not on a row");
        }
        else
        {
            NSLog(@"long press on table view at section %d row %d", indexPath.section, indexPath.row);
            CGPoint p = [gestureRecognizer locationInView: self.myTable];
            NSIndexPath *indexPath = [self.myTable indexPathForRowAtPoint:p];
            if (indexPath != nil)
            {
                if([self becomeFirstResponder])
                {
                    UIMenuItem *delete = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteFileListItem:)];
                    menu = [UIMenuController sharedMenuController];
                    [menu setMenuItems:[NSArray arrayWithObjects:delete, nil]];
                    [menu setTargetRect:[self.myTable rectForRowAtIndexPath:indexPath] inView:self.myTable];
                    [menu setMenuVisible:YES animated:YES];
                }
            }
        }
    }
}

I have also implemented following methods

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(deleteFileListItem:))
    {
        return YES;
    }
    return NO;
}

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

and

- (void)deleteFileListItem:(id)sender
{
    // Will perform action here
}

Please let me know if anything is missing or I am doing wrong.


回答1:


I've been successful when attaching a long press gesture recognizer to each cell, not the entire table view. My guess is that that's the issue here.



来源:https://stackoverflow.com/questions/19402641/uimenucontroller-on-uitableview-does-not-show-up-in-spite-of-all-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!