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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With