简体   繁体   中英

Add UISwitch to only one cell in TableView

Im trying to add a UISwitch to only one cell in my table view heres the code :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FormCell *cell = (FormCell *) [tableView dequeueReusableCellWithIdentifier: @"FormCell"];
    if(cell == nil) cell = [[FormCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FormCell"];

    if(indexPath.row == 3)
    {
        UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 9, 50, 50)];
        [mySwitch addTarget: self action: @selector(flip:) forControlEvents:UIControlEventValueChanged];
        [cell.contentView addSubview:mySwitch];

        [[UISwitch appearance] setOnTintColor:[UIColor colorWithRed:163.0/255.0 green:12.0/255.0 blue:17.0/255.0 alpha:1.0]];
    }

    return cell;
}

its working, the problem is when i scroll the tableview up or down, it duplicate the UISwitch, but in the end or in the beginning of the table view...

Any help?

Remember cells get reused. You're better off creating a custom UITableViewCell with its own identifier. Do your custimzation there.

UITableView is highly optimized, and one of the main optimizations is reusing table cell objects whenever possible. This means that there's no permanent one-to-one mapping between your table rows and UITableViewCell objects.

Thus, the same instance of a cell object can get re-used for multiple rows. Once the row for a cell scrolls off-screen, the cell for that row goes into the "recycle" pile and may get reused for another on-screen row.

By creating Switch objects and adding them to cells, every time row three comes on-screen you're adding it again, to whatever Cell object the table happens to "dequeue" for row 3.

If you're going to add things to reusable cells, you have to have corresponding code that resets the Cell back to default when it gets reused for another table row.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  FormCell *cell = (FormCell *) [tableView dequeueReusableCellWithIdentifier: @"FormCell"];

  if(cell == nil){    
      cell = [[FormCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FormCell"];
  }
 else
  {
    for (UIView *subview in [cell subviews]) 
    {
        [subview removeFromSuperview];
    }
  }

if(indexPath.row == 3)
{
    UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 9, 50, 50)];
    [mySwitch addTarget: self action: @selector(flip:) forControlEvents:UIControlEventValueChanged];
    [cell.contentView addSubview:mySwitch];

    [[UISwitch appearance] setOnTintColor:[UIColor colorWithRed:163.0/255.0 green:12.0/255.0 blue:17.0/255.0 alpha:1.0]];
}

return cell;
}

This will not duplicate the UISwitch on the table scroll The other method is setting the reuseIdentifier to nil . Hope this helps.

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