I’m having a problem. I have a Custom UITableViewCel, the cell contains a slider which changes a value on a label on the cell. When clicking on the cell and then moving the table, the value replicates itself on another cell and then changes the value to a different cell, resetting it’s value to 0.
For demonstration purposes:
First setting the value

Clicking on a random cell then returns:
A totally different cell with the same data that was not put there.

And then when returning back to the cell where the value was first set:

The value is back to 0
Can anyone help me here:
My Slider value changed code;
labelSliderVal.text = [NSString stringWithFormat:@"%1.0f", sliderSlider.value];
if(sliderSlider.value < 30)
{
self.backgroundColor = [UIColor redColor];
}
else if(sliderSlider.value > 60)
{
self.backgroundColor = [UIColor greenColor];
} else {
self.backgroundColor = [UIColor blueColor];
}
And my UITableViews didSelectRowAtIndexPath
Commented out
/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}*/
CellForRowAtIndexPath:
- (CustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"customCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nibObjs = [[NSBundle mainBundle] loadNibNamed:@"CustomCellView" owner:nil options:nil];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
for(id currentObj in nibObjs)
{
if([currentObj isKindOfClass:[CustomCell class]])
{
cell = (CustomCell *)currentObj;
}
}
}
GradeToolAppDelegate * appDelegate = [UIApplication sharedApplication].delegate;
Module *aModule = [appDelegate.modules4 objectAtIndex:indexPath.section];
AssessmentDetail *anAssess = [aModule.assessmentDetails4 objectAtIndex:indexPath.row];
cell.sliderSlider.tag = indexPath.row;
cell.labelAssessment.text = [NSString stringWithFormat:@"%@", anAssess.assessmentName4];
cell.labelAssessmentType.text = [NSString stringWithFormat:@"%@", anAssess.assessmentType4];
cell.labelWeighting.text = [NSString stringWithFormat:@"%@", anAssess.assessmentWeighting4];
cell.labelDueDate.text = [NSString stringWithFormat:@"%@", anAssess.assessmentDueDate4];
return cell;
}
Initialization
In
tableView:cellForRowAtIndexPath:,In
sliderValueChanged:,Code for
totalForSection:,Initial Answer
Well this is happening because of reusable cells. You will need to store the state of the contents and update the cell accordingly in
tableView:cellForRowAtIndexPath:. On slider value change,Now both the model and the label have been changed. So next time cell is reused, the label should get the right value.