切り替え時にTableViewCell内のUISwitchで 'アクティブ'から '無効'に変更したい場合や、切り替えたときにテーブルビューのすべてのデータが消える。私は特定のセルのテキストを変更する方法がわからないので、私は 'リロードデータ'を使用しています。TableViewCellのUISwitch - 切り替え時にセルのテキストを変更する
FYIの 'current item'は、BOOLプロパティ 'itemEnabled'を持つコアデータエンティティです。
スイッチは「編集モード」でのみ表示されます。
私は「ディテール・ビュー・コントローラの内のテーブルビューセルにUISwitchを有する:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSString *cellDetail = nil;
static NSString *EnabledCellIdentifier = @"Enabled";
cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:EnabledCellIdentifier] autorelease];
UISwitch* actSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[cell setEditingAccessoryView:actSwitch];
[actSwitch addTarget:self action:@selector(actSwitchChanged:) forControlEvents:UIControlEventValueChanged];
if ([[currentItem valueForKey:@"itemEnabled"] boolValue]) {
cellDetail = @"Active";
actSwitch.on = YES;
} else {
cellDetail = @"Disabled";
actSwitch.on = NO;
}
[actSwitch release];
cell.textLabel.text = cellDetail;
return cell;
}
をIアクションを受信するための方法を有する:
- (void)actSwitchChanged:(id)sender {
UISwitch* swEnabled = (UISwitch*)sender;
NSManagedObjectContext* itemContext = [currentItem managedObjectContext];
currentItem.itemEnabled = [NSNumber numberWithBool:swEnabled.on];
NSError *error = nil;
if (![itemContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
reloadDataの使用場所がわかりません – Brian