Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. @interface MyTVC : UIViewController
  2.  
  3. @property (strong, nonatomic) NSMapTable switchesToIndexPaths;
  4.  
  5. @end
  6.  
  7. @implementation MyTVC
  8.  
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. // Initialise the map table
  12. self.switchesToIndexPaths = [NSMapTable weakToStrongObjectsMapTable];
  13.  
  14. ....
  15. }
  16.  
  17. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  18.  
  19. // Create the cell as usual
  20. // Map the switch of the cell with the indexpath
  21. [self.switchesToIndexPaths setObject:indexPath forKey:cell.switch];
  22. [cell.switch addTarget:self @selector:(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
  23.  
  24. // Do the other stuff and return the cell
  25. }
  26.  
  27. - (void)switchValueChanged:(UISwitch *)switch {
  28. // Get the indexpath from the switch
  29. NSIndexPath *indexPath = [self.switchesToIndexPaths objectForKey:switch];
  30.  
  31. if (indexPath != nil) {
  32. // You will now know exactly which row and section this switch was for
  33.  
  34. }
  35. }
  36.  
  37.  
  38. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement