代码之家  ›  专栏  ›  技术社区  ›  Vinod Radhakrishnan

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

  •  0
  • Vinod Radhakrishnan  · 技术社区  · 7 年前

    我正在一个tableview数据源函数(索引处的行的单元格)中以编程方式创建UISwitch。当我滚动表格视图时,关闭状态开关出现故障(UI)出现两轮。连接开关的屏幕截图。 enter image description 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 = [[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;
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Vinod Radhakrishnan    7 年前

    在创建switch之前,我也遇到了同样的问题,并用以下代码进行了修复

    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.
      }
    
        2
  •  0
  •   AshokPolu    7 年前

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

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