How to trigger an action from a custom button in a UITableViewCell

I created a subclass of UIButton so that I have a checkbox on the iPhone. This flag is part of a subclass of UITableViewCell . My question is, how can I apply an action to a pressed button and implement this method in a table view controller?

I tried using the addTarget:action: method, but this does not seem to work. One option is to use an application delegate, but this is not a good programming practice.

+4
source share
2 answers

Well, I implemented the same functionality. This is how I implemented it.

  • Created a custom cell.

  • Perform the button action only in the cell class.

  • Declare the protocol (CustomCellDelegate) in the cell and create the delegate property (CustomCellDelegate) of the cell.

  • In cellForRowAtIndexPath: the table view controller method sets the table view controller (self) to delegate the property of the cell.

  • Implement delegates in the controller.

  • Call delegate methods from the button action method in the cell.

Let me know if you understand this or I need to post the code.

+2
source

Make sure the button receives a touch event. You can verify this by overriding

 - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event 

or

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 

Write down something or set a breakpoint on any of them. If your button does not receive touches (which I think is not the case), try setting exclusiveTouch in TableView and / or TableViewCell to NO.

0
source

All Articles