Ios UI开关状态更改重叠
我在tableview单元格中的Ios UI开关状态更改重叠,ios,ios7,uiswitch,Ios,Ios7,Uiswitch,我在tableview单元格中的ui开关有问题 该开关通常处于On(接通)位置,然后当变量改变其值时,开关转到Off(断开)位置,但我有一个问题,就像第二排的开关: 这是进入CellForRowatineXpath部分的简单代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdenti
ui开关
有问题
该开关通常处于On(接通)位置,然后当变量改变其值时,开关转到Off(断开)位置,但我有一个问题,就像第二排的开关:
这是进入CellForRowatineXpath部分的简单代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
tableView.backgroundColor = [UIColor clearColor];
tableView.opaque = NO;
tableView.backgroundView = nil;
[tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage.png"]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if ([self.title isEqualToString:@"Test"]) {cell.textLabel.text = [self.tester objectAtIndex: [indexPath row]];cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.accessoryType = UITableViewCellAccessoryNone;
if (([indexPath row] == 5) || ([indexPath row] == 4)){
NSArray *segmentItems = [NSArray arrayWithObjects:@"Down", @"Up", nil];
UISegmentedControl *testSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentItems];
testSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
testSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
testSegmentedControl.tintColor = [UIColor grayColor];
testSegmentedControl.frame = CGRectMake(160, 10, 150, 30);
[testSegmentedControl setTag:[indexPath row]];
[[cell contentView] addSubview:testSegmentedControl];
[testSegmentedControl addTarget:self
action:@selector(testing:)
forControlEvents:UIControlEventValueChanged];
}
else {
mySwitch = [ [ UISwitch alloc ] initWithFrame: CGRectMake(200, 10, 0, 0) ];
mySwitch.tag = [indexPath row];
mySwitch.onTintColor = [UIColor grayColor];
if ([indexPath row] == 0){
if ([asd41 isEqualToString:@"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
if ([indexPath row] == 1){
if ([asd40 isEqualToString:@"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
if ([indexPath row] == 2){
if ([asd42 isEqualToString:@"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
if ([indexPath row] == 3){
if ([asd76 isEqualToString:@"False"]) mySwitch.on = NO;
else mySwitch.on = YES;
}
[ cell addSubview: mySwitch ];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
[mySwitch addTarget:self action:@selector(switch1:) forControlEvents:UIControlEventValueChanged];}
}
在重复使用单元时,您可能会不断添加切换。只有在实际alloc/init单元格时才添加开关。@rmaddy是的,你是对的。与“[self.tableView重载数据];”一起生效我在另一个上加了一个开关。如何在不重新加载所有tableview的情况下更新交换机状态?问题不在于调用
reloadData
。问题出在cellForRow…
中。每个单元格仅添加一次开关。细胞被重新利用。不要为每次重复使用添加另一个开关。在my cellforrow中。。我只为我在问题中使用代码的每个单元格添加一个开关。在问题中显示完整的cellForRow…
方法。