我在tableview单元格中遇到UISwitch
的问题。
此开关通常处于On位置,然后当变量更改其值时,开关转到Off位置,但我遇到问题,如第二行中的开关:
这是进入cellForRowAtIndexPath部分的简单代码:
- (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];}
}