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.
精彩评论