简体   繁体   中英

UISwitch in UITableViewCell

I have some UISwitches inside UITableViewCells. However this table consist of multiple section. When scrolling down the table the newly created cells are sometimes updated with invalid switches which are not belong to the underline section also. The switch interchanging is not in not predictable order its just randomly adding switches to the cells. However its a weird situation.

This is what I have done.

 if(indexPath.section == 1){
       switch (indexPath.row) { 
        case 0:
         cell.textLabel.text = @"Text 1";
         switch1 = [[UISwitch alloc] initWithFrame:CGRectZero];
         cell.accessoryView = switch1;

         [cell.contentView  addSubview :switch1];

         break;
        case 1:
         cell.textLabel.text = @"Text 2";
         switch2 = [[UISwitch alloc] initWithFrame:CGRectZero];

         cell.accessoryView = switch2;
         [cell.contentView  addSubview :switch2];
         break;
        case 2:
........

More sections

......

So what can be the issue of that?

Thank You.

If you are dequeueing table cells for reuse (as you are supposed to), this could happen. If some table cells are supposed to have certain subviews, and others aren't, then you should use a different reuse identifier for the different types. So make a reuse identifier for table cells that have a UISwitch, and a separate reuse identifier for table cells that don't have a UISwitch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM