I have one tableviewController with Custom cells which containing a label and a Switch. I save the state of the Switch in CoreData
- (IBAction)roomSwitch:(id)sender {
NSLog(@"Switch wurde betätigt");
NSManagedObjectContext *context = [app managedObjectContext];
UISwitch *switcher = sender;
NSInteger rowInIndexPath = switcher.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowInIndexPath inSection:0];
Raumattribute *att=[self.fetchedResultsController objectAtIndexPath:indexPath];
att.switch = [NSNumber numberWithBool:switcher.on];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSLog(@"Schalter: %@", att.switch);
}
This Works ->
2012-03-28 16:43:34.657 Book-App[40011:11903] Switch: 1
2012-03-28 16:43:37.373 Book-App[40011:11903] Switch wurde betätigt
2012-03-28 16:43:37.377 Book-App[40011:11903] Switch: 0
In my cellForRowAtIndexPath i have
[cell.raumSwitch addTarget:self action:@selector(roomSwitch:) forControlEvents:UIControlEventValueChanged];
which calls the roomSwitch:
Now my problem is that if I change a switch, another switch will change its state also. How can I tell the Switch to which row it belongs.
My second Problem is if I change the view, all switches changed to off. How can I show the actual state which is saved in CoreData?
I tried:
cell.roomSwitch = [managedObject valueForKey:@"switch"];
but it crashes.