Reputation:
In a tableviewcell I have a UISlider. If I move the slider knob, go back to my previous view, and then return back to the table view, the knob on the slider returned back to zero but I see a "ghosting" of the knob where I had previously moved the slider too.
I clear the context view on the slider object in cellForRowAtIndexPath: and reload the table in viewDidAppear.
Anyone know how to fix this? It's quite annoying. I put the slider code down below if that helps at all.
// Setup slider
CGRect sliderFrame = CGRectMake(15, 56, 230, 0);
UISlider *slider = [[UISlider alloc] initWithFrame:sliderFrame];
slider.clearsContextBeforeDrawing = YES;
[slider addTarget:self action:@selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
[slider addTarget:self action:@selector(sliderStopped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:slider];
[slider release];
I appreciate it! Thanks!
Upvotes: 0
Views: 534
Reputation: 38728
It is most likely that you are creating a new UISlider
over the old UISlider
which gives the ghosting effect.
Two possible solutions
Tag the UISlider
when you add it to a cell.
Subclass UITableViewCell
and add a UISlider
to it's content view and keep a reference to it with an ivar.
To do 1 simply tag the UISlider
when you add it to the contentView. Then when you get a cell again you try getting the view back first or you create it a fresh.
const int sliderViewTag = 99;
UISlider *slider = [cell.contentView viewWithTag:sliderViewTag];
if (!slider) {
CGRect sliderFrame = CGRectMake(15, 56, 230, 0);
slider = [[UISlider alloc] initWithFrame:sliderFrame];
slider.clearsContextBeforeDrawing = YES;
[slider addTarget:self action:@selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
[slider addTarget:self action:@selector(sliderStopped:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:slider];
[slider release]; slider = nil;
}
Although 2 is a little more involved it is my preferred method but I am sure there are some great examples of how to do it. There is some great docs by Apple so check them out Table View Programming Guide specifically look at the section A Closer Look at Table-View Cells
Upvotes: 1