0

TableViewCell 内の UISwitch でテキストを「アクティブ」から「無効」に変更したいのですが、スイッチが変更されると、テーブル ビューのすべてのデータが消えます。特定のセルのテキストを変更する方法がわからないため、「データの再読み込み」を使用しています。

参考までに、「現在のアイテム」は、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;

}

アクションを受け取る方法があります:

- (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();
}

}

4

4 に答える 4

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";
}
于 2011-02-16T01:09:45.580 に答える
1

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

于 2010-11-19T23:05:36.870 に答える
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";

}

于 2010-11-20T16:29:02.910 に答える
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!)
}
于 2015-10-26T22:12:33.443 に答える