0
  1. UITableViewがあります
  2. すべてのセルにUISwitchがあります。
  3. 私のコアデータは値で埋められています。
  4. @"OFF"値を持つswitchValueという名前のコアデータ属性の1つ。

今私はする必要があります:

  1. ユーザーがUISwitchをONに設定した場合。
  2. このアクションは、その行のswitchValue属性の値を@"ON"に変更します。

私の簡単なコードは次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [[object valueForKey:@"courseCode"] description];
    cell.detailTextLabel.text = [[object valueForKey:@"courseName"] description];
UISwitch *theSwitch = [[UISwitch alloc]init];

    cell.accessoryView = theSwitch;

    NSLog(@"%@",[[object valueForKey:@"switchValue"] description]);

    if ([[[object valueForKey:@"switchValue"] description]isEqualToString: @"ON"]) {

        theSwitch.on = YES;

    }else{

        theSwitch.on = NO;

    }


    [theSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];

    return cell;

}

そしてアクションは:

-(void) switchChanged: (UISwitch *) sender{


    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];

    if (sender.on) {

        How can I change the value?

    }else{

    }

}
4

2 に答える 2

1

管理対象オブジェクトを取得したら、その値を設定するだけです。

NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
[object setValue:@"ON" forKey:@"switchValue"];

これらの変更をディスクに保存したい場合は、ある時点で管理対象オブジェクトのコンテキストも保存する必要があります。

于 2012-08-08T21:03:18.820 に答える
1

おそらく、Core Data モデル マネージャー (モデルを作成するグラフ) のコード生成機能を使用する必要があります。

ただし、次の方法でいつでも値を取得できます。

[object valueForKey:@"foo"]

そしてそれらを設定します

 [object setValue:value forKey:@"foo"];
于 2012-08-08T21:03:34.827 に答える