【问题标题】:Saving USSwitch to CoreData将美国切换到核心数据
【发布时间】: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"];

但它崩溃了。

【问题讨论】:

    标签: ios xcode core-data


    【解决方案1】:

    您可以做的是为每个与 Row 对应的开关添加一个 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】:

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

      每次触发 switch 事件时,对象已经在触发的单元格中。

      【讨论】:

        【解决方案3】:

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

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

        但是没有用。

        【讨论】:

        • 我自己找到了解决方案 mySwitch.on = [[managedObject valueForKey:@"schalter"] boolValue];感谢您的帮助!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多