触摸了细胞的哪个成分

时间:2015-03-20 06:36:54

标签: ios objective-c

我有一个包含2个图像的单元格。当用户触摸特定图像时,我想使用NSLog来说明单击了哪个图像。

目前,我只获得了被选中的单元格。如何才能获得触摸的图像?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"CELL CLICKED WAS %ld",indexPath.row);
}

3 个答案:

答案 0 :(得分:0)

实现您自己的自定义UITableviewCell。

在其中实施touchesBegantouchesMovedtouchesEnded

touchesBegan中,您可以获取touch个对象,获取它的位置并查看它是否在UIImage内。

像这样的东西。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
        UITouch *aTouch = [touches anyObject];
        CGPoint touchPoint = [aTouch locationInView:self.superview];
CGRect imageRect = myImage.frame;
if(CGRectContainsPoint(imageRect,touchPoint)
{
NSLog(@"here");
}

}

答案 1 :(得分:0)

您必须分别在图片上添加任何UIControl,以便知道选择了哪个图片:

UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTouched)];
[yourImageView addGestureRecognizer:tapGesture];

-(void)imageTouched{
    NSLog(@"Image 1 Clicked");
}

对第二张图片做同样的事情..... !!

答案 2 :(得分:0)

您可以在创建单元格时为每个图像添加点击手势识别器

UITapGestureRecognizer *tap = nil;
    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnImageA:)];
    [cell.imageViewA addGestureRecognizer:tap];
    [tap release];

    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapOnImageB:)];
    [cell.imageViewB addGestureRecognizer:tap];
    [tap release];

并将每个图像的手势事件实现为 -

    - (void) handleTapOnImageA:(UITapGestureRecognizer *) recognizer {
        if(recognizer.state == UIGestureRecognizerStateEnded)
        {
            NSLog(@"Image A Touched");
        }
    }

- (void) handleTapOnImageB:(UITapGestureRecognizer *) recognizer {
        if(recognizer.state == UIGestureRecognizerStateEnded)
        {
            NSLog(@"Image B Touched");
        }
    }