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