25

目前我遇到了一个问题,我想在我的 UITableViewCell 上的 UIImageView 被点击时执行操作。

问题:我该怎么做?任何人都可以给我看代码或任何教程吗?

提前致谢!

4

5 回答 5

46

这实际上比你想象的要容易。您只需要确保在 imageView 上启用用户交互,并且可以向其添加轻击手势。这应该在实例化单元格时完成,以避免将多个点击手势添加到同一图像视图中。例如:

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTapMethod:)];

        [self.imageView addGestureRecognizer:tap];
        [self.imageView setUserInteractionEnabled:YES];
    }

    return self;
}

- (void)myTapMethod:(UITapGestureRecognizer *)tapGesture
{
    UIImageView *imageView = (UIImageView *)tapGesture.view;
    NSLog(@"%@", imageView);
}
于 2012-07-20T10:22:16.453 回答
6

试试这个

//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;
[imageView addGestureRecognizer:tap];

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

确保你有....

imageView.userInteractionEnabled = YES;
于 2012-07-20T10:26:11.673 回答
3

您可以使用 customButton 代替 UIImageView

于 2012-07-20T08:43:43.907 回答
0

轻击手势识别器可能会占用大量内存,并可能导致页面上的其他内容中断。我个人建议您创建一个自定义表格单元格,在自定义单元格框架中插入一个按钮,然后在表格视图代码中将 self.customtablecell.background.image 设置为您想要的图像。这样,您可以为按钮分配一个 IBaction 以使其推送到您想要的任何视图。

于 2012-07-20T09:51:29.520 回答
0

使用ALActionBlocks在块中执行操作

__weak ALViewController *wSelf = self;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) {
    NSLog(@"pan %@", NSStringFromCGPoint([weakGR locationInView:wSelf.view]));
}];
[self.imageView addGestureRecognizer:gr];
于 2016-12-27T14:43:44.973 回答