UIswitch in a table cell reused

前端 未结 3 1804
孤街浪徒
孤街浪徒 2021-01-25 21:46

I have a problem with uiswitch in my UITableViewCell that whenever i change a switch value in a specific cell that belongs to a specific section. All other sections

相关标签:
3条回答
  • 2021-01-25 22:24

    As already pointed out by others, you're adding multiple switches to each cell. FTFY:

    UISwitch *switchController = nil;
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
        switchController = [[UISwitch alloc] initWithFrame:CGRectZero];
        switchController.tag = 'swch';
        CGRect switchFrame = switchController.frame;
        //set its x and y value, this you will have to determine how to space it on the left side
        switchFrame.origin.x = 50.0f;
        switchFrame.origin.y = 10.0f;
        switchController.frame = switchFrame;
        [switchController addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:switchController];
    }
    else 
    {
        switchController = (UISwitch*)[cell.contentView viewWithTag: 'swch'];
        NSAssert(switchController != nil, @"how?");
    }
    
    NSString *str = [[self.SwitchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    NSLog(@"string in cell%@", str);
    
    switchController.on = [str isEqualToString:@"ON"];
    
    return cell;
    
    0 讨论(0)
  • 2021-01-25 22:24

    If you look, I think you will find that you are building up multiple switches and on each cell.

    As cells get reused, you add a new switch but do not remove the old one. I think this my be at least part of the cause of your problem.


    UPDATE

    I think I would handle this a bit differently than others. I think it will be easier for @AlanoudAldrees to remove the old switch.

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }
    else
    {
        for (UIView *view in cell.subviews)
            if ([view isKindOfClass:[UISwitch class]])
                [view removeFromSuperview];
    }
    
    0 讨论(0)
  • 2021-01-25 22:49

    You have two main problems:

    1. This code is wrong:

        [cell addSubview:switchController ];
      

      Never add a subview to the cell; add it only to the cell's contentView.

    2. You are adding the switch every time through cellForRowAtIndexPath:. But these cells are being reused. So some cells already have the switch. Thus you are adding many switches to some cells.

    0 讨论(0)
提交回复
热议问题