1

我必须UITableView显示一些列表。实施UILongPressGestureRecognizer以接听电话,在此我想显示删除、上传等操作的菜单。

以下是实现

// Registering for long press event

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

长按我的控制功能

- (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];
                }
            }
        }
    }
}

我还实现了以下方法

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

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

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

请让我知道是否有任何遗漏或我做错了。

4

1 回答 1

0

将长按手势识别器附加到每个单元格而不是整个表格视图时,我已经成功了。我的猜测是这就是这里的问题。

于 2013-10-16T17:57:01.090 回答