ios - UITableView 上未调用长按手势

标签 ios objective-c xcode uitableview uilongpressgesturerecogni

在 iOS 应用程序中,我有一个 UITableView(不是 UITableViewController 的一部分),我想使用以下方法在其上检测自定义 UITableViewCells 上的长按:

- (void)viewDidLoad
{
    myTableView.delegate=self;
    myTableView.dataSource=self;

    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCaptured:)];
    longTap.minimumPressDuration=0.5f;
    longTap.delegate=self;
    [myTableView addGestureRecognizer:longTap];
    [super viewDidLoad];
}

-(void)longTapGestureCaptured:(UILongPressGestureRecognizer *)gesture
{
    NSLog(@"Long tap"); // never called
}

但是,当我长按时,永远不会调用 longTapGestureCaptured。如何解决?

最佳答案

我试过你的代码。它 100% 为我工作。但是您的代码中的小改动是...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //Create cell here
    UITableViewCell *cell;
    cell= (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    //Add gesture to cell here
    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTapGestureCaptured:)];
    longTap.minimumPressDuration=1.5f;
    longTap.delegate=self;
    [cell addGestureRecognizer:longTap];

    cell.textLabel.text = @"Name";//Send your data here

    return cell;

}

-(void)longTapGestureCaptured:(UILongPressGestureRecognizer *)gesture
{
    NSLog(@"Long tap"); // never called
}

关于ios - UITableView 上未调用长按手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50649265/

相关文章:

ios - UITextField leftView 和 rightView 重叠问题 - iOS13

ios - Swift Playground 执行已中止

iphone - 防止在 UIWebview 上捏合缩放和双击缩放

ios - 在UIDatePicker中设置今天的日期而非设备日期

iphone - iOS 6 - 区分 iPhone 5 和其他设备?

ios - Xcode iOS 仪器 :

objective-c - 将属性分配给 sudzc 中的对象

iOS 应用程序发布 - 相同的 Bundle ID 但不同的开发者帐户

objective-c - Mac开发构建基本界面

xcode - 未正确添加文件夹到Xcode项目