title: UISegmentedControl inside a UITableViewCell - segmented control disappears when table reloads
body: <p>I've searched the forum, but have not found anything that works for my situation. I am still new at this, so it is probably something simple, but I can't seem to figure it out.</p>
  
  <p>I have a UITableViewController that has several sections. I have created a custom class called a SSSDataEntryCell that allows the user to enter text. However, in one of the cells, I want to replace it with a UISegmentedControl. The code works the first time the table is displayed (the control appears, I can select a segment and the value changes), but on any type of reload event (rotation, scrolling where the cell is not visible, etc.), the segmented control no longer appears. </p>
  
  <p>Do I need to recreate the segmented control every time? That seems inefficient, especially since it is an ivar. I have tried releasing the subview of the cell before adding and a number of other suggestions on stackoverflow, but nothing seems to fix it. Thanks in advance! </p>
  
  <p>Here is the relevant part of my .h</p>
  
  <pre><code>@interface SSSEditDetailViewController : UITableViewController &lt;UITextFieldDelegate&gt;
  {
      // lets us set the sex (male, female)
      UISegmentedControl *sexSwitch;
  }
  </code></pre>
  
  <p>Here is the relevant part of the .m</p>
  
  <pre><code>- (void)viewDidLoad
  {
      [super viewDidLoad];
  
      // Load the NIB file for our custom cell
      UINib *nib = [UINib nibWithNibName:@"SSSDataEntryTableCell" bundle:nil] ;
  
      // Register the NIB which contains the cell
      [[self tableView] registerNib:nib forCellReuseIdentifier:@"SSSDataEntryTableCell"] ;
  
      //
      // set up the sex switch if needed
      //
      if ( sexSwitch == nil )
      {
          NSArray *values = [[NSArray alloc] initWithObjects:kSSSMale, kSSSFemale, nil] ;
          sexSwitch = [[UISegmentedControl alloc] initWithItems:values] ;
          [sexSwitch addTarget:self action:@selector(changeSex:)  
                    forControlEvents:UIControlEventValueChanged] ;
          [sexSwitch setSegmentedControlStyle:UISegmentedControlStyleBar] ;
      }
  }
  
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      SSSDataEntryTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SSSDataEntryTableCell"] ;
  
      if ( [indexPath section] == 0 )
      {
          [[cell dataField] setDelegate:self] ;
          [[cell dataField] setOurPath:indexPath] ;
  
          [[cell dataField] setText:[person name]] ;
          [[cell dataField] setPlaceholder:@"Name"] ;
      }
      else if ( [indexPath section] == 1 )
      {
          // Sex switch;
          [cell setAccessoryType:UITableViewCellAccessoryNone] ;
  
          // get cell frame and put the sex switch in its place
          [sexSwitch setFrame:[cell frame]] ;
          [sexSwitch setAutoresizingMask:UIViewAutoresizingFlexibleWidth] ;
  
          [[cell contentView] addSubview:sexSwitch] ;
      }
  
      return cell ;
  }
  </code></pre>