保存美国切换到核心数据

时间:2012-03-28 14:55:21

标签: ios xcode core-data

我有一个带有自定义单元格的tableviewController,其中包含一个标签和一个Switch。 我在CoreData中保存Switch的状态

- (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"];

但它崩溃了。

希望有人了解我的问题。

3 个答案:

答案 0 :(得分:0)

我认为最简单的解决方案是将uitableviewcell子类化为原型,并在该子类中将IBOutlet作为一个开关并存储指向managedobject的指针。每次配置单元格时,都要根据该对象设置对象和切换值。

每次触发切换事件时,您都将对象放在它触发的单元格内。

答案 1 :(得分:0)

您可以为每个与行对应的开关添加Tag属性。这样您就可以确切地知道哪个开关/在哪一行上单击了。要更改切换状态,您需要cell.roomSwitch.isOn = YEScell.roomSwitch.isOn = NO

编辑:如果使用BOOL值将其存储在Core Data中,则类型为NSNumber。要更改切换,您需要cell.roomSwitch.isOn = [managedObject.switch boolValue]

对于UIButton,您可以更清楚地了解如何使用标记并知道此示例中更改了哪一行的切换,这基本上是相同的: Detecting which UIButton was pressed in UITableView

答案 2 :(得分:0)

我想我得到了它的工作,他只改变了敲击开关

- (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);

}

但我仍然不知道如何显示coreData中保存的状态。

也许你可以提供一些我能做到的代码吗?

在我继承了Switch之前我试过

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

但它没有用。