如何区分UITableViewCell的哪个部分已被点击

时间:2012-06-17 11:13:43

标签: iphone ios uitableview

我使用自定义UITableView创建了UITableViewCell。我的单元格左侧有一个UIImageView,右侧有UITextView

UITableViewController内,我在tableview cellForRowAtIndexPath中设置了图片和文字。

一切都很好,但现在我需要实施didSelectRowAtIndex,我需要区分单击的UIImageViewUITextView是否已被点击。

假设图像点击表示删除操作和其余的单元格编辑操作。

5 个答案:

答案 0 :(得分:48)

不是将手势识别器添加到每个单独的单元格,而是可以在表格视图中添加一个,并确定从用户触摸的点选择了哪个单元格,然后确定用户是否触摸了图像或单元格。 / p>

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

@interface MyTableViewController() <UIGestureRecognizerDelegate>
@end

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

    - (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

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

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

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

答案 1 :(得分:5)

您可以继承UITableViewCell并覆盖touchesEnded

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    [super touchesEnded:touches withEvent:event];

    UITouch *touch = [touches  anyObject];
    CGPoint location = [touch locationInView:self];

    UIView *hitView = [self hitTest:location withEvent:event];

    if (hitView == myImageView) ...;
    if (hitView == myTextView) ...;
}

您需要保留对UIImageViewUITextView的引用(它们应该是您单元格的属性)。

您当然可以覆盖touchesBegan而非touchesEnded,具体取决于您希望实现的功能。

答案 2 :(得分:1)

一个非常抽象和一般的答案是做以下事情 对于每个UIImage和UILabel,您可以将其标记添加为indexPath.row

//When creating the label and image add a recognizer to them
label.tag = indexPath.row;
imageView.tag = indexPath.row;

然后在每个图像和标签上添加UITapGestureRecognizer,就像这样

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                                 action:@selector(handleTap:)];
    [label addGestureRecognizer:recognizer];
    [imageView addGestureRecognizer:recognizer];
}

- (void) handleTap:(UITapGestureRecognizer*)recognizer
{
    UIView *view = recognizer.view;
    int row = view.tag;
    if ([view isKindOfClass:[UILabel class]]) {
        //Row is row
        //and the label is pressed
    }

    if ([view isKindOfClass:[UIImageView class]]) {
        //Row is row
        //and the imageview is pressed
    }
}

答案 3 :(得分:1)

将图像设为UIButton。触发按钮动作时,您知道用户点击了图像(不会选择单元格)。如果选择了单元格,则表示用户在该行的其他位置(包括文本视图或标签)进行了点击。

另外,将按钮的标签属性设置为,例如单元格的行索引,以便您可以知道哪个行的图像被点击。

答案 4 :(得分:0)

你有两个选择是实施: - 1 - 在“uitableviewcell”中添加UITapGestureRecognizer并使其指向图像视图 并将“indexpath”作为参数传递给选择器,并使委托将其传递给tableviewcell

UILabel *label = =[UILabel alloc]init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTap)]     autorelease];
[label addGestureRecognizer:tapGesture];


-(void)labelTap{
[delegate performselector@selector(labeltapped:)withobject:indexpath];
}

2-第二种方法是检查[imageview或label]类型的DidSelectRowAtIndexPath的发件人; 但我更喜欢第一种方式

相关问题