views:

1357

answers:

3

Hello all.

I have a question that is driving me crazy. It is all about updating, in realtime, an UITableViewCell with the value of an UISlider that is inside another UITableViewCell.

The problem

The cell is updated but It seems that the size of the Label in the cell does not addapt to the size of the value represented. I will try to explain better.

The UISlider has a minimumValue of -100 and a maximum value of 100. So, the first time the TableView is loaded, the cell.detailText.text shows 0.0% witch is displayed ok, but when I move the slider and the value is bigger, for example 55.5%, the label can not hold this extra characters and it displays "55....."

This is how I create the cell that holds de UISlider Value create the UISlider in the cellForRowAtIndexPath delegate method


cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ProductCellIdentifier] autorelease];
cell.textLabel.text=@"Performance";
cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f%%", slider.value];
cell.tag=1002;

This is how I create de UISlider, in the cellForRowAtIndexPath method:


cell = [[[UITableViewCellalloc] initWithFrame:CGRectZero                                                           reuseIdentifier:ProductCellIdentifier] autorelease];

slider = [[UISlideralloc] initWithFrame:CGRectMake(90, 12, 200, 25)];

slider.maximumValue = 100;
slider.minimumValue = -100;
slider.continuous = TRUE;

[slideraddTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];

[cell.contentView addSubview:slider];
cell.tag=0;
[slider release];

And this is what I do in the sliderChanged method in order to update the cell.


UITableViewCell *celda= (UITableViewCell *)[[self tableView] viewWithTag:1002];
celda.detailTextLabel.text = [NSString stringWithFormat:@"%.1f%%", slider.value];

I suspect that the solution is about the use of the reloadData method of the UITableView but not sure. I have tried inserting a self.tableView.reloadData in the sliderChanged method but I got then the UISlider does not move and finally I got this exception:
Terminating app due to uncaught exception 'NSRangeException', reason: ' -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

Thanks in advance for your help
Javi

+1  A: 

Why are you using currencyStyleStringFromNumber: when you update the cell's value? I would think you'd get better results using the same kind of initializer you do when you first put the slider's value into the cell, that is:

cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f%%", slider.value];
Tim
Tim, this is exactly as I have the code. I typed it incorrectly. I have updated the description of the problem. Thanks
monti
+1  A: 

The way you access the cell (by tag) might not get you the cell you expect. Didn't you originally assign the tag 1002? Check the value of celda. And then, just to check, set the value of the cell's label to something less obscure.

Nikolai Ruhe
Sorry Nikolai, as Tim noted, I have pasted the code of other class. As you can see in the corrected version, I am accesing the cell with tag 1002. So it is supposed that this is the correct way to access the cell.
monti
A: 

the problem is that detailTextLabel width is fixed and computed only when the label is going to be drawn in the cell. so, when you initialize the label with "0.0%" there is room for exactly 4 characters, and you cannot visualize correctly anything with more than that; extra charactes are either trimmed with "..." or the label just doesn't display anything.

i worked around this issue by forcing the label to contain additional spaces at the beginning of the string like this:

while ([string length] <= 4)
      string = [NSString stringWithFormat:@" %@",prestring];
cell.detailTextLabel.text = string;

in this way, my slider can visualize correctly numbers from 0 to 999 correctly; since you have more characters just adjust the length desired.

koda