- 我有UITableView
- 在所有單元中都有一個UISwitch。
- 我的核心數據是由值填充。
- 具有@「OFF」值的名爲switchValue的核心數據屬性之一。
現在我需要:如何更改核心數據中的記錄值
- 如果用戶設定的UISwitch爲ON。
- 該操作會將該行的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{
}
}
設置他們,但我必須這樣做,如果用戶改變UISwitch上。 – NamshanNet 2012-08-08 21:27:00
我不確定你在問什麼。 – 2012-08-08 22:54:50