5

I have a button in a tableView Cell that I need to respond to touch events. Normally this would easily be solved by

    [cell.playButton addTarget:self action:@selector(playButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

inside of -tableView:cellForRowAtIndexPath:

The problem I am having is that my button is inside of a custom UITableViewCell subclass and is also the subview of a view that I am creating inside of that class..

for example:

    UIView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    self.theView = view;
    [self addSubview:view];
    [view release];


    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    playButton.frame = CGRectMake(5, 5, 100, 30);
    [playButton setImage:[UIImage imageNamed:@"button_playvid.png"] forState:UIControlStateNormal];
    [playButton setImage:[UIImage imageNamed:@"button_playvid_active.png"] forState:UIControlStateHighlighted];
    self.playButton = playButton;
    [self.theView addSubview:playButton];

When the button is a subview of a view that I create within the custom UITableViewCell then the cell is highlighted instead of the button being pressed so the target that I setup in -tableView:cellForRowAtIndexPath is never called. Not sure why.. any help?

Thanks,

Joel

PS. I realize that there probably isn't a practical reason to create a background view exactly like this since there already is one. This is just example code that I'm using to explain a problem I'm having.. :D

Thanks for Looking!

Joel Bell
  • 2,718
  • 3
  • 26
  • 32
  • Edit your question with "playButtonPressed:" method implementation. – Krishna Feb 14 '12 at 05:46
  • 1
    Are you saying you are creating a UIButton in your view controller and adding it as a subview of both your view controller's view AND in a cell of a uitableview (which is also added to the same view controller's view)?? That can't be a good idea... – MGA Feb 14 '12 at 05:49
  • add your button in to your new [view addSubview:playButton] – Hiren Feb 14 '12 at 06:06
  • playButtonPressed: is just an NSLog() statement telling me if that code ever gets called. The problem is it doesn't so it doesn't matter what the playButtonPressed: implementation is right? – Joel Bell Feb 14 '12 at 06:09

4 Answers4

9

The UIView you are adding is actually a UIImageView. From the docs:

New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.

I think that either userInteractionEnabled cascades to subviews or the superview will not pass the touch to its subviews in this case, so your button will not be receiving touches. Set your image view to have .userInteractionEnabled = YES and you should be fine.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • It Works!! Thanks for your help!! Glad I accidentally kept it as a UIImageView when I created the sample code haha. – Joel Bell Feb 14 '12 at 07:33
  • No problem. This was a perfectly formed question, by the way - clear example, easy to reproduce, all information present, self contained - keep it up! – jrturton Feb 14 '12 at 08:07
1

I had the same problem and tried EVERYTHING. The thing that finally did it was implementing the method

(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Just return whatever the height is of your custom cell (look in the NIB file)

Jeff Grimes
  • 2,300
  • 1
  • 23
  • 23
  • Thanks for posting this tip. I had the same problem and had check all the views ``userInteractionEnabled`` property was set correctly more times than I'd like to admit. Turns out I'd overridden ``tableView:heightForRowAtIndexPath:`` and was returning 0 for some unknown reason. – Col Apr 03 '15 at 12:37
1

Recently I've ran into such issue with legacy code. The reason was that earlier we used to add subviews to custom UITableViewCell instance itself and now we have to add to contentView of the cell instance.

I fixed it by changing:

addSubview(increaseButton)

to

contentView.addSubview(increaseButton)

Of course, isUserInteractionEnabled should be true.

Paul B
  • 3,989
  • 33
  • 46
0

Instead of button, you could try the same with an image. You could define which method to be called when the image is clicked.

Alen Alexander
  • 725
  • 6
  • 22