如何区分 UITableViewCell 的哪一部分被点击 [英] how can I distinguish which part of UITableViewCell has been clicked

查看:40
本文介绍了如何区分 UITableViewCell 的哪一部分被点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有自定义 UITableViewCellUITableView.我的单元格左侧包含一个 UIImageView,右侧包含一个 UITextView.

I have created a UITableView with a custom UITableViewCell. My cell includes one UIImageView on the left and UITextView on the right.

UITableViewController 中,我在tableview cellForRowAtIndexPath 中设置了图像和文本.

Inside UITableViewController, I set both image and text in tableview cellForRowAtIndexPath.

一切正常,但现在我需要实现 didSelectRowAtIndex 并且我需要区分单元格的 UIImageViewUITextView 是否已被点击.

Everything shows fine but now I need to implement didSelectRowAtIndex and I need to distinguish if UIImageView or UITextView of the cell has been clicked.

比方说,图片点击代表删除操作和单元格编辑操作的其余部分.

Let's say, image clicking is representing delete action and the rest of the cell editing action.

推荐答案

与其为每个单独的单元格添加手势识别器,您可以在表格视图中添加一个并根据用户触摸的点确定选择了哪个单元格,然后判断用户是触摸了图片还是单元格.

Rather than adding the gesture recognisers to each individual cell, you can add one to the table view and determine which cell was selected from the point of the users touch, and then determine if the user touched the image or the cell.

首先确保您的控制器采用 UIGestureRecognizerDelegate 协议.

First make sure your controller adopts the UIGestureRecognizerDelegate protocol.

@interface MyTableViewController() <UIGestureRecognizerDelegate>
@end

然后在视图加载时将 UIGestureRecognizer 添加到 UITableView.

Then add the UIGestureRecognizer to the UITableView when the view loads.

    - (void)viewDidLoad
{
    [super viewDidLoad];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    singleTap.delegate = self;
    singleTap.numberOfTapsRequired = 1;
    singleTap.numberOfTouchesRequired = 1;
    [self.tableView addGestureRecognizer:singleTap];
}

此委托方法确定是否应执行 handleTap: 方法.如果它可以从用户触摸中找到indexPath,则返回YES,否则返回NO.

This delegate method determines if the handleTap: method should be executed. If it can find an indexPath from the users touch, then it returns YES otherwise it returns NO.

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    UITableView *tableView = (UITableView *)gestureRecognizer.view;
    CGPoint p = [gestureRecognizer locationInView:gestureRecognizer.view];
    if ([tableView indexPathForRowAtPoint:p]) {
        return YES;
    }
    return NO;
}

一旦我们确定了用户是否点击了一个单元格,handleTap: 方法就会被调用,然后决定用户是触摸了图像还是单元格的任何其他部分.

Once we have determined if the user has clicked in a cell, the handleTap: method is called, which then decides if the user touched the image, or any other part of the cell.

- (void)handleTap:(UITapGestureRecognizer *)tap
{
    if (UIGestureRecognizerStateEnded == tap.state) {
        UITableView *tableView = (UITableView *)tap.view;
        CGPoint p = [tap locationInView:tap.view];
        NSIndexPath* indexPath = [tableView indexPathForRowAtPoint:p];
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        CGPoint pointInCell = [tap locationInView:cell];
        if (CGRectContainsPoint(cell.imageView.frame, pointInCell)) {
            // user tapped image
        } else {
            // user tapped cell
        }
    }
}

这篇关于如何区分 UITableViewCell 的哪一部分被点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆