现在我需要:
我的简单代码是:
- (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{
}
}
答案 0 :(得分:1)
获得托管对象后,只需设置其值即可。
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
[object setValue:@"ON" forKey:@"switchValue"];
如果您希望将这些更改保存到磁盘,您还必须在某些时候保存您的托管对象上下文。
答案 1 :(得分:1)
您应该使用Core Data模型管理器的代码生成工具(创建模型的图形)。
但是,您始终可以通过以下方式获取值:
[object valueForKey:@"foo"]
并使用
设置它们 [object setValue:value forKey:@"foo"];