Created
January 29, 2015 08:18
-
-
Save B0bcat/482d2b160c877c17278a to your computer and use it in GitHub Desktop.
<3 Maptables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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