Question

so i have a dynamic list and each cel contains a stepper and a label with different tags. When i use the stepper, the label has to refresh itself to the value of the stepper. the problem is that (i think ) the sender is sending incorrect values, or taking another stepper's values (from other cell). for example sometimes i tap the stepper on the cell [0] and its value changes as well as the label, but when i press the stepper of cell [1], instead of add 1 (int the stepper), it takes the value of the stepper previously tapped and refreshes its value... and sometimes it says NO THANK YOU I WILL DO WHATEVER I WANT haha, Here's the code to fulfill the list

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellOrder" forIndexPath:indexPath];

    UILabel *cantidad = (UILabel*)[cell viewWithTag:2];
    UILabel *precio = (UILabel*)[cell viewWithTag:3];
    UILabel *nombre = (UILabel*)[cell viewWithTag:4];
    UIStepper *stepper = (UIStepper*) [cell viewWithTag:5];
    PFObject *object = [self.myOrder objectAtIndex:indexPath.row];
    cantidad.text = [NSString stringWithFormat:@"%.0f", stepper.value];
    precio.text = [NSString stringWithFormat:@"%d",([[object objectForKey:@"precio"] intValue] * [cantidad.text intValue])];
    nombre.text = [object objectForKey:@"nombre"];
    PFImageView *image = (PFImageView *)[cell viewWithTag:1];
    image.file = [object objectForKey:@"imagen"];
    [image loadInBackground];

    [self.mailEnviar addObject:precio.text];

    return cell;
}

the var cantidad is the label that i need to refresh...

so my code of refreshing is this one

- (IBAction)valueChanged:(UIStepper *)sender 
{
    UIView *view = [sender superview]; //to recover the superview 
    self.selectedCell = (UITableViewCell *) [[view superview] superview]; //this is making a cast to get the cell of the stepper (the one that was pressed) 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:self.selectedCell ]; // to get the index path and help me refresh that cell in specific

    self.selectedRow = [[NSArray alloc]initWithObjects:indexPath, nil];
    [self.tableView reloadRowsAtIndexPaths:self.selectedRow withRowAnimation:UITableViewRowAnimationAutomatic]; // to refresh the selected cell
}

Hope you can help me out...

Était-ce utile?

La solution

In general when you have a table view - the changes you do, you should do in the data model for this table view. So when stepper is tapped you need to update not the label, but the data source and then ask table to reload such cell.

So you need to save each stepper value in the object array, update that value in your valueChanged method and reload the cell (which you already do).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top