【问题标题】:How can i dynamically change the UISwitch of UITableView?如何动态更改 UITableView 的 UISwitch?
【发布时间】:2012-11-25 00:38:40
【问题描述】:

我有一个UItableView,其中每个UITableViewCell 都包含一个UISwitch。现在我的问题是我什么时候点击一个开关,那么我如何OFFUITableViewCell 的其他开关

在我的代码中,我已经创建了视图,我可以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];
    }
}

【问题讨论】:

    标签: iphone ios uitableview delegates uiswitch


    【解决方案1】:

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

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

    您需要一个实例变量来跟踪每个开关的状态。创建一个数组来保存 YES 和 NO 值。在下面的代码中,我将假设有一个名为 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;
        }
    }
    

    【讨论】:

    • 感谢您的快速回答。请给我一个例子,因为我不明白我该怎么做..
    • 为您的cellForRowAtIndexPath: 显示您的代码(更新您的问题),展示您如何为每一行设置开关。还要显示当开关的值发生变化时处理事件的代码。
    • 我有问题。现在请检查我要做什么。
    • 那个代码全错了。您使用单个 ivar 进行开关,另一个用于维护状态。您需要跟踪每一行的开关状态。给我一点,我会用一些更新的代码来更新我的答案。
    猜你喜欢
    • 1970-01-01
    • 2015-04-17
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2019-08-26
    • 2012-01-25
    • 1970-01-01
    相关资源
    最近更新 更多