It took me a while to figure out how to use prototype cells. If you want to access the user interface elements inside the prototype cell, you need to subclass UITableViewCell and assign it to the prototype cell in Interface Builder. Then you need to define the IBOutlet properties manually, for example:
@interface OptionSwitchCell : UITableViewCell @property (weak, nonatomic) IBOutlet UISwitch* switchControl; @end
After that, you can connect the interface elements by dragging the control from the element to the property definition in the assistant window. Instead, IBActions can be defined inside the owner of the View Controller. You can control drag and drop from an interface element to the View Controller header file and create an action. Inside the implementation of an action, you probably want to know which cell triggered 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 appropriate cell path and index (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 bizarre, I have not yet found a better solution. Hope this helps.
source share