Guest User

Untitled

a guest
Jul 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. // Establish what position the switch should be in
  2. if([info.state isEqualToString:@"on"]){
  3. mySwitch.selected = TRUE;
  4. } else {
  5. mySwitch.selected = FALSE;
  6. }
  7.  
  8. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  9. {
  10. static NSString *CellIdentifier = @"Cell";
  11. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  12. if (cell == nil) {
  13. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
  14. }
  15.  
  16. // Configure the cell...
  17. [self configureCell:cell atIndexPath:indexPath];
  18. return cell;
  19. }
  20.  
  21. - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
  22. InfoObject *info = [self.fetchedResultsController objectAtIndexPath:indexPath];
  23. cell.textLabel.text = info.name;
  24.  
  25. // Use a custom UISwitch that holds a pointer to the row it is associated with
  26. NamedUISwitch *mySwitch = [[[NamedUISwitch alloc] initWithFrame:CGRectZero] autorelease];
  27. mySwitch.indexPath = indexPath;
  28. [mySwitch addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventValueChanged];
  29. // And of course we need to know what the state is
  30. NSLog(@"switchState:%@",info.state);
  31.  
  32. // Establish what position the switch should be in
  33. if([info.state isEqualToString:@"on"]){
  34. mySwitch.selected = TRUE;
  35. } else {
  36. mySwitch.selected = FALSE;
  37. }
  38.  
  39. cell.accessoryView = mySwitch;
  40. }
  41.  
  42. - (void) changeState:(id)sender {
  43. InfoObject *info = [self.fetchedResultsController objectAtIndexPath:((NamedUISwitch*)sender).indexPath];
  44.  
  45. if(((NamedUISwitch*)sender).on){
  46. info.state = @"on";
  47. } else {
  48. info.state = @"off";
  49. }
  50.  
  51. NSError *error;
  52. if (![self.context save:&error]) {
  53. NSLog(@"Whoops, couldn't save state: %@", [error localizedDescription]);
  54. //TODO: alertview
  55. }
  56. }
  57.  
  58. // Establish what position the switch should be in
  59. if([info.state isEqualToString:@"on"]){
  60. mySwitch.on = YES;
  61. } else {
  62. mySwitch.on = NO;
  63. }
Add Comment
Please, Sign In to add comment