6

So far I´ve only needed to implement prototype cells with pre-defined designs (normal, subtitle, etc.) and it hasn´t been a problem.

Now I need to implement prototype cells which contain some controls like a segmented switch, a switch or any other. The problem is that I haven´t been able to find out how the actions triggered are implemented and how are they related to the control. Also I heven´t found any example of how different prototype cells inside a single UITableViewController are implemented.

I know it´s kind of a generic question, but I´d appreciate some pointers here. Maybe someone knows about some documentation, tutorial, etc. Well, any help would do,

Thnaks in advance.

Marcal
  • 1,371
  • 5
  • 19
  • 37

1 Answers1

10

It took me also a while to understand how to use the prototype cells. If you want to access the user interface elements inside a prototype cell, you have to create a subclass of UITableViewCell and assign it to the prototype cell in Interface Builder. Then you have to define the IBOutlet properties manually e.g.:

@interface OptionSwitchCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UISwitch* switchControl;
@end

After that, you can connect the interface elements through control-dragging from the element to the property definition in the assistant view. The IBActions instead can be defined inside the owning View Controller. You can control-drag from a interface element to the View Controller header file and create an action. Inside the action implementation you will likely want to know which cell was been triggering the action. I do it like this:

@implementation SomeTableViewController

- (IBAction)toggleActivity:(id)sender {
    OptionSwitchCell* cell = (OptionSwitchCell *)[sender superview].superview;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    ...
}

@end

Another solution for finding the corresponding cell and index path (by jrturton):

- (IBAction)toggleActivity:(id)sender {
    CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView]; 
    NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
    OptionSwitchCell* cell = (OptionSwitchCell *)[self.tableView cellForRowAtIndexPath:hitIndex];
    ...
}

Although this is a bit quirky, I haven't found a better solution so far. Hope that helps.

Sbhklr
  • 2,693
  • 1
  • 17
  • 20
  • 1
    A few minutes ago, I found this video (and its second part): http://youtu.be/0AsChJk422c it´s more or less what you explained but he/she uses a delegate. The same principle, but it looks a little more clean. Thanks for your answer ! – Marcal Apr 21 '12 at 12:20
  • It's indeed a cleaner solution. Thanks for the pointer. Although a bit much of boilerplate code for all the protocols. – Sbhklr Apr 21 '12 at 12:48
  • 1
    A better way to find the row of the control can be found in my answer here: http://stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number/9274863#9274863 – jrturton Apr 21 '12 at 15:29