UILongPressGestureRecognizer不适用于tableview

Vaq*_*ita 3 xcode objective-c uitableview ios ios5

我在ViewDidLoad方法的桌面视图中添加了一个UILongPressGestureRecognizer.我添加了这个以检测我的代码中的长按表格视图.但它永远不会奏效.在ViewDidLoad中,我添加了以下代码:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
                                      initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 2.0; //seconds
lpgr.delegate = self;
[self.resultTableView addGestureRecognizer:lpgr];
[lpgr release];
Run Code Online (Sandbox Code Playgroud)

我还添加了这个方法:

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:self.resultTableView];

    NSIndexPath *indexPath = [self.resultTableView indexPathForRowAtPoint:p];
    if (indexPath == nil) {

        NSLog(@"long press on table view but not on a row");
    }
    else {


        NSLog(@"long press on table view at row %d", indexPath.row);
    }


}
Run Code Online (Sandbox Code Playgroud)

请帮帮我解决这个问题?

Pra*_*d G 6

你的代码正在运行.我认为你必须UIGestureRecognizerDelegate在.h文件中添加委托或如何声明resultTableView我的意思是你以编程方式定义或使用.xib文件.检查一次.

我试过这样的.

     resultTableView = [[UITableView alloc] init];
     resultTableView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420) style:UITableViewStylePlain];
    resultTableView.rowHeight = 100.0;
    resultTableView.delegate=self;
     resultTableView.dataSource=self;
    [self.view addSubview:resultTableView];

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
                                          initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 2.0; //seconds
    lpgr.delegate = self;
    [resultTableView addGestureRecognizer:lpgr];
    [lpgr release];
Run Code Online (Sandbox Code Playgroud)