Skip to content

Instantly share code, notes, and snippets.

@B0bcat
Created January 29, 2015 08:18
Show Gist options
  • Save B0bcat/482d2b160c877c17278a to your computer and use it in GitHub Desktop.
Save B0bcat/482d2b160c877c17278a to your computer and use it in GitHub Desktop.
<3 Maptables
@interface MyTVC : UIViewController
@property (strong, nonatomic) NSMapTable switchesToIndexPaths;
@end
@implementation MyTVC
- (void)viewDidLoad {
[super viewDidLoad];
// Initialise the map table
self.switchesToIndexPaths = [NSMapTable weakToStrongObjectsMapTable];
....
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Create the cell as usual
// Map the switch of the cell with the indexpath
[self.switchesToIndexPaths setObject:indexPath forKey:cell.switch];
[cell.switch addTarget:self @selector:(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
// Do the other stuff and return the cell
}
- (void)switchValueChanged:(UISwitch *)switch {
// Get the indexpath from the switch
NSIndexPath *indexPath = [self.switchesToIndexPaths objectForKey:switch];
if (indexPath != nil) {
// You will now know exactly which row and section this switch was for
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment