1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

UISegmentedControl disappearing from UITableViewCell

Discussion in 'iPhone/iPad Programming' started by cnstoll, Jan 18, 2011.

  1. macrumors 6502

    So..I've managed to rig up a table view cell to display a segmented control. It actually works pretty well. The problem is that if I try to reload anything in the table (even a specific row with reloadRowsAtIndexPaths) the segmented control disappears.

    This is how I currently have it set up where in the cellForRowAtIndexPath delegate method:

    // segControl is declared in the header		
    segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Option1", @"Option2", nil]];
    segControl.frame = CGRectMake(-1.0f, -1.0f, 302.0f, 46.0f);
    [segControl addTarget:self action:@selector(segChanged:) forControlEvents:UIControlEventValueChanged];
    [cell.contentView addSubview:segControl];
    Any ideas why this would be happening or how to fix it? Thanks
  2. Moderator


    Staff Member

    So, you have one segControl instance for every single row in the table?
  3. macrumors 68030


    Show all the code for cellForRowAtIndexPath
  4. cnstoll, Jan 19, 2011
    Last edited: Jan 19, 2011

    macrumors 6502

    Nope. Only for one specific row in one section. It's a settings dialog and most of the rows are different.

    I don't have the rest of my code with me, but I just realized something. I think I may be using the same cellIdentifier for all the cells.... I bet using a specific cell identifier for that cell..and maybe all the different types of cells for that matter..will fix the problem.

  5. Moderator


    Staff Member

    Possible, but nothing certain until I can see the entire code for cellForRowAtIndexPath:.
  6. macrumors 6502

    I figured it out. It's because of where I was calling "reloadRowsAtIndexPaths". I was calling it inside of the selector for changing the segmented control, because I am trying to modify text in a certain cell based on the selected segment.

    In the cellForRowAtIndexPath method I set the selected index of the segmented control. That calls the selector, and that fires off the reloadRowsAtIndexPaths...but that happens before the view appears, and apparently that's not good.

    Solution? Use a little boolean to see if it's the initial call or not...and if not...then it's safe to reload the rows.

Share This Page