【发布时间】:2012-03-28 14:55:21
【问题描述】:
我有一个带有自定义单元格的 tableviewController,其中包含一个标签和一个开关。 我将 Switch 的状态保存在 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);
}
这个作品->
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
在我的 cellForRowAtIndexPath 我有
[cell.raumSwitch addTarget:self action:@selector(roomSwitch:) forControlEvents:UIControlEventValueChanged];
调用 roomSwitch:
现在我的问题是,如果我改变一个开关,另一个开关也会改变它的状态。如何告诉 Switch 它属于哪一行。
我的第二个问题是,如果我改变视图,所有开关都变为关闭。如何显示保存在 CoreData 中的实际状态?
我试过了:
cell.roomSwitch = [managedObject valueForKey:@"switch"];
但它崩溃了。
【问题讨论】: