UITablevlewCell 内 UIImageView 上的 UITapGestureRecognizer 未被调用

发布于 2024-12-09 12:23:27 字数 797 浏览 3 评论 0原文

我目前有一个自定义 UITableViewCell,其中包含 UIImageView 并尝试在 UIImageView 上添加 UITapGestureRecognizer,但没有成功。这是代码片段。

//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[tap release];

// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
    RKLogDebug(@"imaged tab");
}

我还在单元格和 UIImageView 的超级视图上设置了 userInteractionEnabled 但仍然没有运气,有什么提示吗?

编辑

我还通过 cell.selectionStyle = UITableViewCellSelectionStyleNone; 删除了单元格的选择;这可能是个问题吗

I currently have a custom UITableViewCell which contains a UIImageView and trying to add a UITapGestureRecognizer on the UIImageView with no luck. here is snippet of the code.

//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[tap release];

// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer {
    RKLogDebug(@"imaged tab");
}

I've also set userInteractionEnabled on the cell and the superview of the UIImageView but still no luck, any hints?

EDIT:

I've also remove cell's selection by cell.selectionStyle = UITableViewCellSelectionStyleNone; Could this be a problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街女流氓 2024-12-16 12:23:27

UIImageView 的用户交互默认情况下处于禁用状态。您必须显式启用它才能响应触摸。

imageView.userInteractionEnabled = YES;

UIImageView's user interaction is disabled by default. You have to enable it explicitly to make it respond to touches.

imageView.userInteractionEnabled = YES;
寂寞美少年 2024-12-16 12:23:27

Swift 3

这对我有用:

self.isUserInteractionEnabled = true

Swift 3

This worked for me:

self.isUserInteractionEnabled = true
喜你已久 2024-12-16 12:23:27

就我而言,它看起来像:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = CELL_ROUTE_IDENTIFIER;
    RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[RouteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:cellIdentifier];
    }

    if ([self.routes count] > 0) {
        Route *route = [self.routes objectAtIndex:indexPath.row];

        UITapGestureRecognizer *singleTapOwner = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(imageOwnerTapped:)];
        singleTapOwner.numberOfTapsRequired = 1;
        singleTapOwner.cancelsTouchesInView = YES;
        [cell.ownerImageView setUserInteractionEnabled:YES];
        [cell.ownerImageView addGestureRecognizer:singleTapOwner];
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

和选择器:

- (void)imageOwnerTapped:(UISwipeGestureRecognizer *)gesture {
    CGPoint location = [gesture locationInView:self.tableView];
    NSIndexPath *tapedIndexPath = [self.tableView indexPathForRowAtPoint:location];
    UITableViewCell *tapedCell  = [self.tableView cellForRowAtIndexPath:tapedIndexPath];

    NSIndexPath *indexPath = [self.tableView indexPathForCell:tapedCell];
    NSUInteger index = [indexPath row];

    Route *route = [self.routes objectAtIndex:index];
}

In my case it looks like :

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = CELL_ROUTE_IDENTIFIER;
    RouteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[RouteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:cellIdentifier];
    }

    if ([self.routes count] > 0) {
        Route *route = [self.routes objectAtIndex:indexPath.row];

        UITapGestureRecognizer *singleTapOwner = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                    action:@selector(imageOwnerTapped:)];
        singleTapOwner.numberOfTapsRequired = 1;
        singleTapOwner.cancelsTouchesInView = YES;
        [cell.ownerImageView setUserInteractionEnabled:YES];
        [cell.ownerImageView addGestureRecognizer:singleTapOwner];
    } else {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

And selector :

- (void)imageOwnerTapped:(UISwipeGestureRecognizer *)gesture {
    CGPoint location = [gesture locationInView:self.tableView];
    NSIndexPath *tapedIndexPath = [self.tableView indexPathForRowAtPoint:location];
    UITableViewCell *tapedCell  = [self.tableView cellForRowAtIndexPath:tapedIndexPath];

    NSIndexPath *indexPath = [self.tableView indexPathForCell:tapedCell];
    NSUInteger index = [indexPath row];

    Route *route = [self.routes objectAtIndex:index];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文