0

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];}

}
4

0 回答 0