How to detect touches on UIImageView of UITableViewCell object in the UITableViewCellStyleSubtitle style
I am using the UITableViewCell
object in the UITableViewCellStyleSubtitle
style (i.e an image view on the left, text label in bold and under that a detail text label) to create my table. Now I need to detect touches on the UIImageView
and also to know the indexpath/cell in which开发者_运维技巧 the image view was clicked. I tried using
cell.textLabel.text = @"Sometext";
NSString *path = [[NSBundle mainBundle] pathForResource:@"emptystar1" ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
cell.imageView.userInteractionEnabled = YES;
But it's not working. Whenever the image is clicked, didSelectRowAtIndexPath:
is called. I don't want to create a separate UITableViewCell
and add a custom button to it. Is there any way to detect touches on the UIImageView
itself?
In your cellForRowAtIndexPath
method add this code
cell.imageView.userInteractionEnabled = YES;
cell.imageView.tag = indexPath.row;
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapped];
[tapped release];
And then to check which imageView
was clicked, check the flag in selector
method
-(void)myFunction :(id) sender
{
UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
NSLog(@"Tag = %d", gesture.view.tag);
}
The accepted solution is currently broken in iOS 5.0. The bug causes the image view's gesture recognizer to never be triggered. Through research on the official developer forums I found that this is a known bug in iOS 5.0. It is caused by an internal implementation that causes -gestureRecognizerShouldBegin:
to return NO. The bug appears when you are setting the gesture recognizer's delegate to the custom UITableViewCell
subclass itself.
The fix is to override -gestureRecognizerShouldBegin:
in the gesture recognizer's delegate and return YES. This bug should be fixed in a future version of iOS 5.x. This is only safe as long as you are not using the new UITableViewCell copy/paste API's.
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
For Swift, in your cellForRowAtIndexPath method add this code
cell.imageView?.userInteractionEnabled = true
cell.imageView?.tag = indexPath.row
var tapped:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "TappedOnImage:")
tapped.numberOfTapsRequired = 1
cell.imageView?.addGestureRecognizer(tapped)
And then to check which imageView was clicked, check the flag in selector method
func TappedOnImage(sender:UITapGestureRecognizer){
println(sender.view?.tag)
}
One way you could do it is by creating a UIImageView from your image and add a gesture recognizer to it. See the example below
//Create ImageView
UIImageView *theImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"emptystar1.png"]];
theImageView.userInteractionEnabled = YES;
//Add Gesture Recognizer
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageSelectedInTable)];
tapped.numberOfTapsRequired = 1;
[theImageView addGestureRecognizer:tapped];
[cell addSubview:theImageView];
//Memory Cleanup
[tapped release];
[theImageView release];
-(void)imageSelectedInTable
{
NSLog(@"Selected an Image");
}
However, you'll now have to layout your cells more since you can't simply use the UIImageView
property of the UITableViewCell
since it's readonly.
Only how additional information, in my case I had to answer to a tap gesture over an image file of a table cell retrieved from NSFetchedResultsController, so we need the indexPath, not the row, so I decided to use the image tapped itself instead of the row information. I defined a variable for the image:
var imgSelected: UIImageView? = nil
I added the gesture recognizer to the imageView (imgZona):
public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Storyboard.CellReuseIdentifier, forIndexPath: indexPath) as! ZonasTableViewCell
let zona = fetchedResultsController.objectAtIndexPath(indexPath) as! Zona
cell.infoZona = zona
let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
cell.imgZona?.addGestureRecognizer(tapGestureRecognizer)
cell.imgZona?.userInteractionEnabled = true
return cell
}
and in the "imageTapped" method I retrieved the image and saved to the variable "imgSelected":
func imageTapped(sender: UITapGestureRecognizer) {
imgSelected = sender.view as? UIImageView
self.performSegueWithIdentifier("MuestraImagen", sender: self)
}
Finally, in the "prepareForSegue" method I sent the image of the viewController dedicate to show the image tapped:
override public func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
case "MuestraImagen":
if let vc = segue.destinationViewController as? MuestraImagenViewController {
if imgSelected != nil {
if let img = imgSelected!.image {
vc.imgPuente = img
}
}
}
default: break
}
}
}
This problem is:
- Add ImageView into TableViewCell
- Event click into ImageView different than event click into TableViewCell
--> The below codes work correctly with me:
cell.imageView.image = [UIImage imageNamed:@"my_image.png"];
[cell.imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[cell.imageView addGestureRecognizer:singleTap];
and "singleTapGestureCaptured" function
- (void)singleTapGestureCaptured:(UITapGestureRecognizer*)gesture{
NSLog(@"Left Image clicked");
}
精彩评论