- UITableViewがあります
- すべてのセルにUISwitchがあります。
- 私のコアデータは値で埋められています。
- @"OFF"値を持つswitchValueという名前のコアデータ属性の1つ。
今私はする必要があります:
- ユーザーが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{
}
}