我有一个 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
类似。您还应该删除 switchView
和 switchCondition
实例变量。
- (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/