2010-11-19 5 views
0

切り替え時に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(); 
} 

}

+0

reloadDataの使用場所がわかりません – Brian

答えて

0

だからUISwtichをサブクラス化した後、アクションメソッドで使用することができるセルのインデックスパスを格納するためのプロパティが含まれ:ここ

- (void)actSwitchChanged:(id)sender { 

TLSwitchInCell* swEnabled = (TLSwitchInCell*)sender; 

currentItem.itemEnabled = [NSNumber numberWithBool:swEnabled.on]; 

UITableViewCell* theCell = [self.tableView cellForRowAtIndexPath:swEnabled.cellIndexPath]; 

theCell.textLabel.text = swEnabled.on ? @"Active" : @"Disabled"; 

}

1

-[UITableView cellForRowAtIndexPath:]で特定のセルを取得できます。セルのインデックスパスを知るだけで済みます(デリゲートメソッドを呼び出さずに、セルのテーブルビューを要求していることにも注意してください)。

+0

インデックスパスはどのように知っていますか?スイッチはアクションメッセージの一部としてそれを送信できますか? – Glynton

+0

基本的にセルのインデックスパスであるスイッチオブジェクトの識別子をいくつか設定できますか? – Glynton

2

は内から細胞を得るためのアプローチでありますサブクラス化を必要としないアクションメソッドUISwitch:

- (void)switchAction:(id)sender { 

    // Cast the sender as a UISwitch 
    UISwitch *aSwitch = (UISwitch *)sender; 

    // Cast the superview of aSwitch to a UITableViewCell 
    UITableViewCell *cell = (UITableViewCell *)aSwitch.superview; 

    // You could get an indexPath as follows (though you don't need it in this case) 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];  

    // Set the text of the label in your cell 
    cell.textLabel.text = aSwitch.on ? @"Active"; : @"Disabled"; 
} 
0

テーマの別のバリエーション!あなたがスイッチボタンを持つ設定フィールドの多くを持っている場合は、このようなトリガーだったスイッチボタンに関連付けられているラベルのテキストをつかむために何かを使用することができます!通常の方法でテキストを設定できます!

@IBAction func switch(sender: UISwitch) { 
    let labelIndex = sender.superview!.subviews.indexOf({$0 is UILabel}) 
    NSLog("The text from the label associated with this switch is %@", 
    (sender.superview!.subviews[labelIndex!] as! UILabel).text!) 
} 
関連する問題