PDA

View Full Version : UISegmentedControl disappearing from UITableViewCell




cnstoll
Jan 18, 2011, 11:52 PM
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



dejo
Jan 19, 2011, 10:00 AM
So, you have one segControl instance for every single row in the table?

PhoneyDeveloper
Jan 19, 2011, 12:02 PM
Show all the code for cellForRowAtIndexPath

cnstoll
Jan 19, 2011, 12:42 PM
So, you have one segControl instance for every single row in the table?

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.

Thoughts?

dejo
Jan 19, 2011, 01:53 PM
Thoughts?

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

cnstoll
Jan 19, 2011, 11:20 PM
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.