1

With a storyboard, you can easily customise a cell, with subviews, and insert actions of those subviews, directly inside the table controller, via - (IBAction)handle:(id)sender;
But is it possible to do something similar, without a storyboard or a nib ?
I have a custom cell, and a custom table controller.

  • I can't insert the action inside the cell implementation, because it's supposed to trigger stuff from the controller.
  • I can use UIGesture inside the controller, at tableView:cellForRowAtIndexPath:, but every time it's called, it will insert a gesture for the cell. Of course, I can manage this...

So I wonder if there is a "simple" way to implement the action of a cell's subview inside the table controller, like the IBAction, without a storyboard or a nib ? Thanks. B.R.

1 Answers1

1

If you already have a custom tableViewCell, you can add an UITapGestureRecognizer to that view.

You have to make a subclass of UITableViewCell and implement the - awakeFromNib Method. There you can add the gesture recognizer to the view inside the cell.

It's not a complete solution, but it could be a good point to start.

Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
  • The thing is, I don't have a storyboard or a nib. I'd like to do everything programmatically. So if I put the tap implementation inside the custom cell class, I can not trigger stuff from the table controller class, like : present a new controller... – user3048615 Nov 29 '13 at 09:11
  • I would recommend creating a NIB file with a single tableViewCell. Than you can use `[UITableView registerNib:forReusableIdentifier:];`. When you then use `- dequeCellForReusaleIdentifier:`, you NIB gets loaded and setup. In the NIB you make a prototype for the cell, add the recognizer and connect outlets and so on. This is the BEST practice. – Julian F. Weinert Nov 29 '13 at 09:18
  • I just read this again and thought I could add some info. If you really don't want to use Nibs you can also register your custom class and do everything programmatically in the class. You then implement a protocol for the cell and set your controller as delegate of the cell in `- tableView:cellForRowAtIndexPath:` – Julian F. Weinert Feb 15 '15 at 17:59