3

So I want three buttons in my tableView cell. I've made a custom cell so I customize the buttons in the custom cell once instead of doing it again and again in cellForRowAtIndexPath

But I want to add a selector to all three buttons, for which I am using

[cell.btn1 addTarget:self action:@selector(firstAction:) forControlEvents:UIControlEventTouchUpInside];

inside cellForRowAtIndexPath. is that the best way to go about it? wont addTarget be initialize and reinitialize itself everytime I scroll through my cells? wont that be an extra overload? is there a better way of doing this that I dont know about?

edit

This is how I figure-out which cell the button belongs to that was pressed

CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.feedTableView];
NSIndexPath *indexPath = [self.feedTableView indexPathForRowAtPoint:buttonPosition];
Community
  • 1
  • 1
Jonathan
  • 2,728
  • 10
  • 43
  • 73

2 Answers2

1

You can set your buttons' target and actions via your storyboard or XIB file. The more interesting problem will be for you to figure out which table cell your button was pressed from.

I suggest subclassing UIButton and adding a "row" or "indexPath" property to your subclassed UIButton, which you set in your table feeding "cellForRowAtIndexPath" method, and from which ("sender") you can extract which row the button was clicked once the button is pressed and your action gets called.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Michael Dautermann, please look at my edit. the current logic dosent work when I use storyboard to assign them the actions. – Jonathan May 15 '13 at 17:32
  • how does your current logic not work? indexPath is bogus or? – Michael Dautermann May 15 '13 at 17:46
  • well it works if I do the selector thing in cellForRowAtIndexPath, but when I use storyboards to connect the actions directly to the buttons, and then press the buttons, nothing happens. – Jonathan May 15 '13 at 18:06
  • If you set a breakpoint on your action, do they fire when you touch a button set up via the storyboard? If not, you might not be connecting the action properly. – Michael Dautermann May 15 '13 at 18:19
  • I guess the thats because to make the custom cells, I am `initWithFrame` the buttons in my custom cell files – Jonathan May 15 '13 at 18:24
  • [You can create a custom tableview cell within your storyboard](http://stackoverflow.com/questions/9245969/in-a-storyboard-how-to-make-a-custom-cell-for-use-with-multiple-controllers). The question I just linked to should show you how. – Michael Dautermann May 15 '13 at 18:28
0

You should probably create a delegate in your custom cell, and have this delegate get fired when any button is clicked. Some code sample:

@protocol CustomCellDelegate
-(void)cell:(UITableViewCell *)cell buttonTapped:(UIButton) atIndexPath:(NSIndexPath *)indexPath;
@end

@interface CustomCell
property (weak) id<CustomCellDelegate> delegate;
@end

@implementation CustomCell
-(void)buttonTapped {
  // fire delegate here
}
@end

I'd also strongly recommend you checkout the free Sensible TableView framework, as it makes handling such custom cells with buttons much easier. I've saved tons of time myself with it for the past few months.

Matt
  • 2,391
  • 2
  • 17
  • 18