0

私はUItableViewすべてUITableViewCellが含まれている場所を持っています。UISwitch今私の質問は、いつ1つのスイッチをクリックするのか、そしてどのようにOFF他のスイッチをクリックするのかということです。UITableViewCell

私のコードでは、すでにビューを作成しており、スイッチを使用できますが、選択したスイッチを除く他のすべてのスイッチを使用ON/OFFしたいと思います。OFF

例またはソースコードの例を挙げて、私を助けてください。

敬具

編集

私のコード:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
        cell.accessoryView = switchview;
        switchCondition = NO;
        [switchview setOn:NO animated:YES];
        [switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged];
        [switchview release];
    }
    if(switchCondition == YES){
    [switchview setOn:YES animated:YES];
    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[cellValueArray objectAtIndex:indexPath.row]];
    return cell;
}

- (void)updateSwitchAtIndexPath:(UISwitch*)sender {
    if(sender.on){
        switchCondition = YES;
        [table reloadData];
    }
}
4

1 に答える 1

1

テーブルのデータソースで使用されるデータモデルを更新してから、テーブル(または少なくとも表示されている行)をリロードします。これにより、各行がリロードされ、各スイッチが最新のデータで更新されます。

編集:コードの更新バージョンは次のとおりです。

各スイッチの状態を追跡するには、インスタンス変数が必要です。YESとNOの値を保持する配列を作成します。以下のコードでは、各行のYES値とNO値を表すオブジェクトで設定されたswitchConditionstypeという名前のインスタンス変数があると想定しています。これはあなたのに似ています。また、インスタンス変数を削除する必要があります。NSMutableArrayNSNumbercellValueArrayswitchViewswitchCondition

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UISwitchView *switch = [[UISwitch alloc] initWithFrame:CGRectZero];
        cell.accessoryView = switch;
        [switchview addTarget:self action:@selector(updateSwitchAtIndexPath:) forControlEvents:UIControlEventValueChanged];
        [switch release];
    }

    UISwitchView *switch = (UISwitchView *)cell.accessoryView;
    switch.tag = indexPath.row; // This only works if you can't insert or delete rows without a call to reloadData
    BOOL switchState = [switchConditions[indexPath.row] boolValue];
    switch.on = switchState; // this shouldn't be animated

    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.textLabel.text = cellValueArray[indexPath.row];

    return cell;
}

- (void)updateSwitchAtIndexPath:(UISwitch*)switch {
    NSInteger row = switch.tag;
    if (switch.on){
        // This switch is on, turn all of the rest off
        for (NSUInteger i = 0; i < switchConditions.count; i++) {
            switchConditions[i] = @NO;
        }
        switchConditions[row] = @YES;
        [self.tableView reloadData];
    } else {
        switchConditions[row] = @YES;
    }
}
于 2012-11-25T00:40:19.063 に答える