开发者

How to detect if user click directly on cell.imageView and not on the label in tableview cell?

In my tableView cell i have:

cell.imageView.image = image;

How can I detect the user click directly on that image, and not t开发者_JAVA技巧he cell.textLabel?


Add a UITapGestureRecognizer to the imageView.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    /* ... */
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.imageView.image = /*...*/

        UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)] autorelease];
        [cell.imageView addGestureRecognizer:tapGesture];
        cell.imageView.userInteractionEnabled = YES;
    }
    /* ... */
}

- (void)imageTapped:(UITapGestureRecognizer *)gesture {
    UITableViewCell *cell = [[[gesture view] superview] superview];
    NSIndexPath *tappedIndexPath = [self.tableView indexPathForCell:cell];
    NSLog(@"Image Tap %@", tappedIndexPath);
}


As some of the comments have pointed out, you should use an UIButton instead. A UIImageView is not designed to handle user interaction out of the box and it's much simpler to just create a button, add a target, and add it as a subview of the cell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜