Sandeep Bansal
Sandeep Bansal

Reputation: 6394

Custom TableViewCell changing values randomly?

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

enter image description here

Clicking on a random cell then returns:

A totally different cell with the same data that was not put there.

enter image description here

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

enter image description here

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;
}

Upvotes: 0

Views: 1124

Answers (1)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

Initialization

NSMutableArray *results = [[NSMutableArray alloc] init];

NSInteger numberOfSections = [myTableView numberOfSections];
for ( int section = 0; section < numberOfSections ; section++ ) {
    NSInteger       numberOfRows   = [myTableView numberOfRowsInSection:section];
    NSMutableArray *sectionResults = [NSMutableArray array];

    [results addObject:sectionResults];

    for ( int row = 0; row < numberOfRows; row++ ) {
        [sectionResults addObject:[NSNumber numberWithFloat:0.0]];
    }
}

In tableView:cellForRowAtIndexPath:,

...
NSArray *sectionResults = [results objectAtIndex:indexPath.section];
NSNumber *number = [sectionResults objectAtIndex:indexPath.row];

slider.value = [number floatValue];

...

In sliderValueChanged:,

- (void)sliderValueChanged:(UISlider *)slider {
    CustomCell *cell = (CustomCell *)slider.superview;
    NSIndexPath *indexPath = [myTableView indexPathForCell:cell];
    NSArray *sectionResults = [results objectAtIndex:indexPath.section];

    ...
    NSNumber *number = [NSNumber numberWithFloat:slider.value];
    cell.sliderValueLabel.text = [NSString stringWithFormat:@"%f", slider.value];

    [sectionResults replaceObjectAtIndex:indexPath.row withObject:number];
    ...
}

Code for totalForSection:,

- (float) totalForSection:(NSInteger)sectionIndex {
    NSArray *sectionResults = [results objectAtIndex:sectionIndex];
    float result = 0.0;

    for (NSNumber *number in sectionResults) {
        result += [number floatValue];
    }

    return result;
}

- (float)sumTotal {
    float result = 0.0;
    NSinteger numberOfSections = [myTableView numberOfSections];
    for ( int i = 0; i < numberOfSections; i++ ) {
        result += [self totalForSection:i];
    }

    return result;
}

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,

- (void)sliderValueChanged:(UISlider *)slider {
    CustomCell *cell = (CustomCell *)slider.superview;
    NSIndexPath *indexPath = [myTableView indexPathForCell:cell];

    AssessmentDetail *anAssessment = [module.assessmentDetails4 objectAtIndex:indexPath.row];
    anAssessment.property = slider.value;
    cell.propertyLabel.text = [NSString stringWithFormat:@"%f", slider.value];
}

Now both the model and the label have been changed. So next time cell is reused, the label should get the right value.

Upvotes: 1

Related Questions