Question

I have a label (quantitylbl) and a stepper inside a dynamic UITableView cell. I am able to update UILabel on stepper value changed, but when I scroll till that cell goes out of the visible screen and scroll back again to display the label , then on stepper value changed, the label is not getting updated. In logs I can see the updated value, but the label is not updated. Help is appreciated. Here is the code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    UIStepper *stepper;
    UILabel *quantitylbl;

    int sectionCount = 0;

    if ([indexPath section] == 1)  sectionCount = 10;  // Manage tags

    switch ([indexPath row])
    {
        case 2:
            cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell2"];
            cell.textLabel.text = @"Quantity per day";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            stepper =[[UIStepper alloc] initWithFrame:CGRectMake(200, 60, 40, 20)];
            [stepper setStepValue:0.25];
            [stepper addTarget:self action:@selector(stepperPressed:) forControlEvents:UIControlEventValueChanged];
            stepper.tag = indexPath.section;

            quantitylbl = [[UILabel alloc] initWithFrame:CGRectMake(160, 12, 40, 20)];

            if ([indexPath section] == 0)
                quantitylbl.text = [self.milk1 objectForKey:@"Quantity"]; //get value from dict
            else
                quantitylbl.text = [self.milk2 objectForKey:@"Quantity"];

            [stepper setValue:[quantitylbl.text doubleValue]];
            quantitylbl.tag = sectionCount + [indexPath row];

            [cell setAccessoryView:stepper];
            [cell addSubview:quantitylbl];

            break;

}

- (IBAction)stepperPressed:(UIStepper *)sender
{
    UILabel *label;
    UITableViewCell* cell =  [sender superview];
    NSString* strval = [NSString stringWithFormat:@"%.2f",sender.value];

    if (sender.tag == 0)
    {
        label = (UILabel *)[cell viewWithTag:2];
        [self.milk1 setValue:strval forKey:@"Quantity"];
        label.tag = 2;
    }
    else
    {
        label = (UILabel *)[cell viewWithTag:12];
        [self.milk2 setValue:strval forKey:@"Quantity"];
        label.tag = 12;  
    }

    if (label != nil)
    {
        // I see correct value here, also label is updated correctly. But when I scroll the
        // label out of screen and bring it back, the UILabel no more gets updated though
        // I see correct values here.    
        label.text = strval; 
    }
}
Was it helpful?

Solution

I think that you use wrong approach. You should update the data only from cellForRowAtIndexPath method. Change the class variable self.milk1 in the stepperPressed method and after call method reloadData on the tableview.

- (IBAction)stepperPressed:(UIStepper *)sender
{
    NSString* strval = [NSString stringWithFormat:@"%.2f",sender.value];

    if (sender.tag == 0)
    {
        [self.milk1 setValue:strval forKey:@"Quantity"];
    }
    else
    {
        [self.milk2 setValue:strval forKey:@"Quantity"];
    }
    [self.tableView reloadData];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top