如何动态更改UITableView的UISwitch



我有一个UItableView,其中每个UITableViewCell都包含一个UISwitch。现在我的问题是我什么时候会点击一个开关,然后我怎样才能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];
    }
}

更新表数据源使用的数据模型,然后重新加载表(或至少加载可见行)。这将导致每一行重新加载,并且每个开关都将使用最新数据进行更新。

编辑:这是代码的更新版本:

您需要一个实例变量来跟踪每个交换机的状态。创建一个数组来保存"是"和"否"值。在下面的代码中,我将假设有一个名为 switchConditions 的类型为 NSMutableArray 的实例变量,该变量已设置了 NSNumber 个对象,表示每行的 YES 和 NO 值。这类似于您的cellValueArray。您还应该摆脱switchViewswitchCondition实例变量。

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

最新更新