【问题标题】:UISwitch off state is not stable while scrolling in tableview在 tableview 中滚动时 UISwitch off 状态不稳定
【发布时间】:2017-10-09 08:50:05
【问题描述】:

我在 tableview 数据源函数之一中以编程方式创建 UISwitch(索引处的行单元格)。当我滚动表格视图时,关闭状态开关出现故障(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;
}

【问题讨论】:

  • 按数组管理单元格的可重用性,您的问题将得到解决
  • @Vinod Radhakrishnan 您也可以在表格单元格类的 prepareForReuse 方法中设置开关的默认值。
  • 我已经更新了我的代码。你能解释一下吗? @iPatel
  • "if ([[displayDict objectForKey:@"status"] isEqualToString:@"ACTIVE"])": 如果开关状态,改变,我清楚地跳你更新displayDict。还有[cell.contentView addSubview:myswitch];NO。避免这种情况,细胞被重复使用(也被称为两次)。为什么不将其添加到自定义单元格中,并在不需要时将其隐藏?
  • @Larme 这是我的错误,它实际上是注释代码。我也会在这里更正它。

标签: ios objective-c uitableview uiswitch


【解决方案1】:

我也遇到过同样的问题,在创建开关之前用下面的代码修复了

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.
  }

【讨论】:

  • 此代码将删除单元格中的所有标签和图像。
  • 已添加屏幕截图。
  • 我遇到的确切问题,检查此代码,如果不起作用,请关注@ipatel cmets
  • 我们应该为 switch 设置标签,然后我们应该从子视图中删除
  • 此代码适用于 uiswitch。但它会删除我的所有其他数据(标签和图像)。我该如何解决?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多