The cell does not update the value as the UISlider value is changed

I have a question that drives me crazy. It's all about updating in real time a UITableViewCell with a UISlider value that is inside another UITableViewCell.

Problem

The cell is updated, but it seems that the size of the label in the cell is not added to the size of the displayed value. I will try to explain better.

UISlider has a minimum value of -100 and a maximum value of 100. Thus, when I first load the TableView, cell.detailText.text shows 0.0%, the witch displays normally, but when I move the slider and the value is greater, for example 55.5%, the label is not can hold these extra characters and the display shows "55 ....."

This is how I create a cell that contains a UISlider Value, creates a UISlider in the delegate method cellForRowAtIndexPath


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 a 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 to update the cell.


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

I suspect the solution is to use the reloadData method of the UITableView, but not sure. I tried to insert self.tableView.reloadData in the self.tableView.reloadData method, but I got it, then UISlider is not moving, and finally I got this exception:
Application termination due to an uncaught exception "NSRangeException", reason: '- [NSCFArray objectAtIndex:]: index (0) outside bounds (0)'

Thank you in advance for your help.
Javi

+4
source share
3 answers

the problem is that the detailTextLabel width is fixed and calculated only when the label is drawn in the cell. therefore, when you initialize the label "0.0%", there is room for exactly 4 characters, and you cannot correctly visualize anything with more than that; additional features are either truncated "...", or the shortcut simply does not display anything.

I worked on this issue, forcing the shortcut to contain extra spaces at the beginning of the line as follows:

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

Thus, my slider can correctly display numbers from 0 to 999; since you have more characters, just adjust the desired length.

0
source

Why are you using currencyStyleStringFromNumber: when updating a cell value? I think you will get better results using the same initializer that you do when you first put the value of the slider in the cell, i.e.:

 cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1f%%", slider.value]; 
+1
source

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

+1
source

All Articles