2012-11-25 35 views
0

我有一个UItableView,每一个UITableViewCell是含有UISwitch。现在我的问题是,当我将在一个开关点击那我怎么才能OFFUITableViewCell我该如何动态更改UITableView的UISwitch?

在我的代码的其他交换机我已经作出的观点和我可以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]; 
    } 
} 

回答

1

更新你的数据模型,然后重新装入表(或至少在可见行)。这会导致每行重新加载,并且每个交换机都会更新最新的数据。

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

你需要一个实例变量来跟踪每一个开关的状态。创建一个数组来保存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; 
    } 
} 
+0

感谢您快速回答。请给我一个例子,因为我不明白我该怎么做.. – Emon

+0

显示你的代码(更新你的问题)为你的'cellForRowAtIndexPath:'显示你如何设置每一行的开关。同时显示当交换机的值发生变化时用于处理事件的代码。 – rmaddy

+0

我有问题。现在请检查我必须做的事情。 – Emon