在 tableview 中滚动时 UISwitch 关闭状态不稳定

UISwitch off state is not stable while scrolling in tableview

我正在 table 查看数据源函数(索引处的行的单元格)之一中以编程方式创建一个 UI 开关。当我滚动 table 视图时,OFF 状态开关发生故障(UI)出现了两轮。附上开关机截图。

感谢您的帮助!

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    UILabel *title = (UILabel*)[cell viewWithTag:80];
    UILabel *description = (UILabel*)[cell viewWithTag:81];
    UIView *innerView = (UIView *)[cell viewWithTag:82];

    innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0]  CGColor];
    innerView.layer.borderWidth = 2.0f;
    NSDictionary *displayDict = scenarioListsArray[indexPath.row];
    title.text =[displayDict objectForKey:@"name"];
    description.text = [displayDict objectForKey:@"description"];    
    UISwitch *myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60, (cell.contentView.frame.size.height/2)-20 , 100, 100)];
    myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
    [cell.contentView addSubview:myswitch];
    myswitch.tag = indexPath.row;
    [myswitch addTarget:self action:@selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];
    if ([[displayDict objectForKey:@"status"] isEqualToString:@"ACTIVE"]) {
        [myswitch setOn:YES animated:YES];

    }
    else
    {
        [myswitch setOn:NO animated:YES];
    }


    return cell;
}

我遇到了同样的问题并在创建开关之前使用以下代码修复了

for(UIView *subView in cell. contentView.subviews){
        if ([subView isKindOfClass:[UISwitch class]]) {
        [subView removeFromSuperview];

    }
    }

static NSString *TableIdentifier = @"YourCellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableIdentifier];

if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
    //place your all UI elements code here.
  }

我已经更新了你的代码,试试这个对你有用,

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    UILabel *title = (UILabel*)[cell viewWithTag:80];
    UILabel *description = (UILabel*)[cell viewWithTag:81];
    UIView *innerView = (UIView *)[cell viewWithTag:82];

    innerView.layer.borderColor=[[UIColor colorWithRed:229/255.0f green:229/255.0f blue:229/255.0f alpha:1.0]  CGColor];
    innerView.layer.borderWidth = 2.0f;
    NSDictionary *displayDict = scenarioListsArray[indexPath.row];
    title.text =[displayDict objectForKey:@"name"];
    description.text = [displayDict objectForKey:@"description"];    
    UISwitch *myswitch = [cell.contentView viewWithTag:indexPath.row+597];
    if(mySwitch == nil){
        myswitch = [[UISwitch alloc]initWithFrame:CGRectMake(cell.contentView.frame.size.width-60,(cell.contentView.frame.size.height/2)-20 , 100, 100)];
       [cell.contentView addSubview:myswitch];
       [myswitch addTarget:self action:@selector(cellButtonClickAction:) forControlEvents:UIControlEventValueChanged];

    }
    myswitch.onTintColor = [UIColor colorWithRed:25/255.0f green:122/255.0f blue:66/255.0f alpha:1];
    myswitch.tag = indexPath.row+597; //597 is extra number added to tag
    if ([[displayDict objectForKey:@"status"] isEqualToString:@"ACTIVE"]) {
        [myswitch setOn:YES animated:YES];
    }
    else
    {
        [myswitch setOn:NO animated:YES];
    }

    return cell;
}