views:

203

answers:

2

I am using apple's custom table view cell code and modifying the drawRect code within the cell's view to look like I want it to. I've changed it to have some UILabels as well as a UIProgressView.

If the data the cell is being built on doesn't have a certain field, I want the UIProgressView to be hidden. This works for a little while, but when a cell gets requeued from scrolling, the progress view will start displaying again, even when I set it to hidden = YES. I've tried just not creating the ProgressView unless the data was there and that didn't work either.

I thought the answer was in the [self setNeedsDisplay] but that doesn't seem to help.

Here is the code for the progressview from drawRect that continues to be displayed:

UIProgressView *c1Progress = [[UIProgressView alloc]initWithFrame:CGRectMake(20.0, 70.0, 280.0, 12.0)];   
float iProgress = (value / target);
c1Progress.progress = iProgress;
if (!dataExists) {
    c1Progress.hidden = YES;
}
[self addSubview:c1Progress];
[c1Progress release];
+1  A: 

You definitely don't want this code in drawRect. You should alloc/init your UIProgressView and add it as a subview in your UITableViewCell subclass init method (and be sure to release it in dealloc).

The logic for setting c1Progress.progress and c1Progress.hidden should go in some method that is only called when value, target, or dataExists changes. If you have a setter method for the data/value in question, that would be a good place for it.

Tom
Thanks, that did it. I moved c1Progress to a class variable, instantiated it in the init method and set the values in the data setter method. This is my first attempt at custom tableviewcell programming and I want to make sure I'm doing it right so I have nicely scrolling tables!
adamweeks
+1  A: 

check dataExists's value... and print it in NSLog and if this piece of code execute every time... remove this from drawRect... initialize and release it only once...

mihirpmehta
dataExists's value is properly set and the code for setting hidden=YES does run. I put a breakpoint on it and it goes through as expected.
adamweeks