How to Detect Touches on UIimageview of UItableviewcell in Swift

How to detect touches on UIImageView of UITableViewCell object in the UITableViewCellStyleSubtitle style

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);
}

Detect Tap on UIImageView within UITableViewCell

Maybe not the ideal solution, but add tags to each of the UIImageViews. Then have an NSArray with the uid's corresponding to the tag values

So somewhere in your code make the array

NSArray *testArray = [NSArray arrayWithObjects:@"uid1", @"uid2", @"uid3", @"uid4", @"uid5", @"uid6", nil];

Then when you're setting up the tableview cells set the tag to the row #

//Set the tag of the imageview to be equal to the row number 
cell.imageView.tag = indexPath.row;

//Sets up taprecognizer for each imageview
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
[cell.imageView addGestureRecognizer:tap];

//Enable the image to be clicked
cell.imageView.userInteractionEnabled = YES;

Then in the method that gets called you can get the tag like this

- (void)handleTap:(UITapGestureRecognizer *)recognizer  
{
NSString *uid = testArray[recognizer.view.tag];
}

How to detect touches on UIImageView of UITableViewCell in Swift?

Try writing it yourself. That is how you learn. Here are some line by line hints:

1: cell.imageView.userInteractionEnabled = YES;
2: cell.imageView.tag = indexPath.row;

3: UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
4: tapped.numberOfTapsRequired = 1;
5: [cell.imageView addGestureRecognizer:tapped];
6: [tapped release];

7: -(void)myFunction :(id) sender
8: {
9: UITapGestureRecognizer *gesture = (UITapGestureRecognizer *) sender;
10: NSLog(@"Tag = %d", gesture.view.tag);
11:}

Hints

  • Lose the semicolons all around
  • Line 1) Swift uses true instead of YES for a positive Boolean
  • Line 3) Type let tapped = UITapGestureRecognizer( into Xcode and look at the choices that pop up for autocomplete. Chose the one with target and action. Fill in target self and action "myFunction:"
  • Line 4) Pretty much identical
  • Line 5) Call addGestureRecognizer(tapped) on cell.imageView
  • Line 6) Not needed, ARC does this for you
  • Line 7) Define a function myFunction that takes a UITapGestureRecognizer called gesture and doesn't return anything
  • Line 9) Line not needed since you already took care of that in the header of your function
  • Line 10) Use println instead of NSLog and use string interpolation to print out gesture.view.tag

Detecting touch inside UITableViewCell subview

I've done similar thing, but it was UILongPressGestureRecognizer. I think there is no big difference (because all touches are received by UITableView). I've added gesture recognizer in controllers viewDidLoad method (NOT IN cell).

- (void) tableViewLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
CGPoint p = [gestureRecognizer locationInView:self.messageTableView];

NSIndexPath *indexPath = [self.messageTableView indexPathForRowAtPoint:p];
if (indexPath == nil)
NSLog(@"long press on table view but not on a row");
else {
UITableViewCell *cell = [self.messageTableView cellForRowAtIndexPath:indexPath];
CGPoint pointInCell = [cell convertPoint:p fromView:self.messageTableView];
}
}

You can change Long press to regular one and try it yourself

How to detect touch inside an UITableViewCell

There are a couple variants to hit the target. In fact it doesn't matter at all how you are doing it at the cell's level. It may be buttons, image view with tap gesture recognizers or custom drawing and proceeding touch events. I will not provide you with code now as you have a lot of code already given and may find a lot more by little search, thought it may be provided by demand. The real "problem" is in transporting message to the controller. For that purpose I've found only two reasonable solutions. First is the notifications and second is the delegations. Note that the notifications method may lead to visible lag between tap and actual event occurrence as sometimes notifications are brought to objects with little delay...

Hope that it helps.



Related Topics



Leave a reply



Submit