0

I am implementing a UITableView, in which each cell has a UIImageView subview. I want this UIImageView to be clickable, so I did the following:

In tableview:cellForRowAtIndexPath function, when I initialize the cell (when unable to dequeue from used ones), I have following initialization for the imageview:

imageView.userInteractionEnabled = YES;   
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(replyTapped:)];      
singleTap.numberOfTapsRequired = 1;        
[imageView addGestureRecognizer:singleTap];

And the cell's userInteractionEnabled property is NO since I don't want the entire cell to be selected, like below:

cell.userInteractionEnabled = NO;

And inside the same class, I have this callback defined:

- (void)imageviewTapped:(UISwipeGestureRecognizer* )tap

but it seems like this imageviewTapped function is never called when I tap on the imageview..

I am wondering if I am doing anything wrong here, and what is a good practice to add TapGestureRecognizer to subview of a cell?

Thank you

Allan Jiang
  • 11,063
  • 27
  • 104
  • 165
  • 2
    userInteraction must be enabled to allow subviews to recieve touches. You can always call `deselectRow` in the `didSelectRow` method for the table so it is never selected to prevent it from being "touched" like you want. – Alex Reynolds Jun 19 '14 at 21:45

1 Answers1

0

I would suggest not changing the setting of userInteractionEnabled for the cell. You run the risk of messing up the functioning of the table view. Leave that alone.

How are you adding your image view to the cell? are you adding it to cell.contentView? (table views are UIViews, but the view that appears on the screen is the cell's content view)

Set the allowsSelection property on the table view to NO if you don't want the user to be able to select cells.

Duncan C
  • 128,072
  • 22
  • 173
  • 272