views:

532

answers:

1

I've added a button to a tableview cell, but I'm having two problems with it:

1) I've invoked a setTarget:action: method on the button, but the action: method never gets called:

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

2) the cell itself ends up getting selected on the touch, invoking didSelectRowAtIndexPath, which I don't want to happen.

I've verified that the button is at least seeing the touch by setting its showsTouchWhenHighlighted property and seeing it "glow" when pressed. But for whatever reason it looks like the touch event is then getting passed up to its cell superview instead of being handled by the button itself.

I've tried adding the button directly to the cell's contentView, as well as to other cell subviews, with no effect. I've also made sure that the button's superview(s) were all touch-enabled.

Any thoughts?

Howard

A: 

There are two ways to do that, the simplest is to add the button to the cell with IB, set a tag number to the button and when you are in the cellForRowAtIndexPath method updating the cell use something like:

UIButton *myButton = (UIButton *)[cellYouAreUpdating viewWithTag:-1969];

being -1969 the tag number you previously set in IB and cellYouAreUpdating the cell reference. After that you will have a reference to the button in the cell and you could set the add target with the proper selector.

Hope this helps.

acidscan