objective-c - UILongPressGestureRecognizer 不适用于 tableview

标签 objective-c ios xcode uitableview ios5

我在 ViewDidLoad 方法中将 UILongPressGestureRecognizer 添加到 TableView 。我添加这个是为了检测代码中 TableView 的长按。但它永远行不通。在 ViewDidLoad 中,我添加了这段代码:

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

我还添加了这个方法:

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


}

请帮我解决这个问题?

最佳答案

您的代码正在运行。我认为您必须在 .h 文件中添加 UIGestureRecognizerDelegate 委托(delegate)或如何声明 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];

关于objective-c - UILongPressGestureRecognizer 不适用于 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11877742/

相关文章:

objective-c - 使用@property 和@synthesese?

ios - iOS 10 中 UITextView 顶部的空白

ios - 使用钥匙串(keychain)存储设备标识符

objective-c - 中文日期的 NSDateFormatter

ios - 设置框架时weakSelf

ios - 在应用商店中添加一个版本以支持以前版本的 iOS

ios - UIButton 不可点击但可接收手势

ios - 如何使用 Parse Swift iOS 8 Xcode 6.4 注销

swift - 如何使用Swift在WebView中查找和突出显示文本

iphone - 在界面构建器的 UILabel 中使用本地化文本?