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 ofYES
for a positiveBoolean
- Line 3) Type
let tapped = UITapGestureRecognizer(
into Xcode and look at the choices that pop up for autocomplete. Chose the one withtarget
andaction
. Fill in targetself
and action"myFunction:"
- Line 4) Pretty much identical
- Line 5) Call
addGestureRecognizer(tapped)
oncell.imageView
- Line 6) Not needed, ARC does this for you
- Line 7) Define a function
myFunction
that takes aUITapGestureRecognizer
calledgesture
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 ofNSLog
and use string interpolation to print outgesture.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
How to Enable a Button in Different Cases in Swift
Parse Codable Classes and Avoid Repetition
Swiftui Change on Multilevel Children Published Object Change
Swiftui Map Overlays Without UIviewrepresentable
Swift Bug? Calling Super Class Method When Subclass with Generic Type
Comma Automatically Being Added to Textfield in Swift
Sharing Button Works Perfectly on iPhone But Crash on Ipad
List Inside Scrollview Is Not Displayed on Watchos
How to Get My Button to Reflect The Bool of My Environmentobject
Error When Running Coreml in The Background: Error Computing Nn Outputs Error
Swiftui Previews - Unexpected Data
Applescript Email Attachment Not Working in Handler
How to Calculate The Camera Position from Vuforia Gl Matrix
Swift 3 Custom Extension of Ns Measurement? Ex. Sheeps to Goats
Swift - Setting Calayer Bounds or Frame Not Working
Cannot Convert Value of Type 'Nsmutablearray' to Expected Argument Type '[Skaction]'
How to Navigate from Initial UIviewcontroller to UIsplitviewcontroller in Swift
How to Stop Firestore Pagination When Searched Data Are Loaded in Tableview