0

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Bashud
  • 266
  • 2
  • 8
  • 20

3 Answers3

0

I think the easiest solution would be to subclass uitableviewcell as a prototype and in that subclass have the IBOutlet to a switch and also store a pointer to the managedobject. Each time the cell is configured set the object and switch value based on that object.

Each time a switch event is fired you have the object already inside the cell that it fired in.

utahwithak
  • 6,235
  • 2
  • 40
  • 62
0

What you can do is add a Tag attribute for each switch that corresponds to the Row. This way you will know exactly which switch/on which row was clicked. To change the state of the switch you need cell.roomSwitch.isOn = YES or cell.roomSwitch.isOn = NO.

EDIT: If you use BOOL value for storing it in Core Data, then the type is NSNumber. To change the switch you need cell.roomSwitch.isOn = [managedObject.switch boolValue]

You can get a more clear idea about using the tags and knowing the switch for which row was changed in this example for UIButton which is basically the same: Detecting which UIButton was pressed in UITableView

Community
  • 1
  • 1
o15a3d4l11s2
  • 3,969
  • 3
  • 29
  • 40
0

I think i got it to work, that he only change the tapped Switch

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  RaumAttributeCell *cell =(RaumAttributeCell *)[tableView dequeueReusableCellWithIdentifier:@"attributCell"];


NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.raumAttributLabel.text = [managedObject valueForKey:@"attributname"];



NamedUISwitch *mySwitch = [[NamedUISwitch alloc] initWithFrame:CGRectZero];
[mySwitch addTarget:self action:@selector(raumSwitch:) forControlEvents:UIControlEventTouchUpInside];


cell.accessoryView = mySwitch;
[cell.contentView addSubview:mySwitch];



mySwitch = [cell.contentView.subviews objectAtIndex:0];
[mySwitch setTag:indexPath.row];

return cell;

}

- (IBAction)raumSwitch:(id)sender {

NSLog(@"Switch wurde betätigt");
NSManagedObjectContext *context = [app managedObjectContext];

UISwitch *switcher = sender;
int row  = switcher.tag;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
Raumattribute *att=[self.fetchedResultsController objectAtIndexPath:indexPath];

att.schalter = [NSNumber numberWithBool:switcher.on];

NSError *error = nil;
if (![context save:&error]) {

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
NSLog(@"Schalter: %@", att.schalter);

}

But i still dont know how to show the the state which is saved in coreData.

Maybe you can provide me some code how i can do that?

Before i subclassed the Switch i tried

cell.roomSwitch.isOn = [managedObject.switch boolValue]

but it didnt worked.

Bashud
  • 266
  • 2
  • 8
  • 20
  • I found the solution by myself mySwitch.on = [[managedObject valueForKey:@"schalter"] boolValue]; THX for your help! – Bashud Mar 29 '12 at 10:07