iphone - 如何动态更改 UITableView 的 UISwitch?

标签 iphone ios uitableview delegates uiswitch

我有一个 UItableView每一个UITableViewCell包含 UISwitch现在我的问题是,当我单击一个开关时,我该如何 OFF UITableViewCell的其他开关

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

最佳答案

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

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

您需要一个实例变量来跟踪每个开关的状态。创建一个数组来保存 YES 和 NO 值。在下面的代码中,我假设有一个名为 switchConditions 的类型为 NSMutableArray 的实例变量,该变量已使用表示 YES 和 NO 的 NSNumber 对象进行设置每行的值。这与您的 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;
    }
}

关于iphone - 如何动态更改 UITableView 的 UISwitch?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13547188/

相关文章:

iphone - GPS 位置在我的应用程序的 iphone 中打开/关闭

ios - 在ios上的其他线程上运行http请求

uitableview - 如何最初在UITableView中选择一行

iPhone:单击按钮后重新启动应用程序?

iphone - 在 Xcode 中使用同名资源

objective-c - 如何在 NSMutableArray 上添加观察者?

ios - 返回表格时保持文本单元格的颜色

ios - iPhone:如何将 inputAccessoryView 修复为 View?

objective-c - 使用键值编码将 JSON 转换为 Objective-C 对象

ios - 在 UIViewController 中使用 UITableView 的搜索栏