2

I have a problem controlling UIStepper in a UITableView with custom cell. When I change one UITextField.text with stepper in (row=0) it works fine, the problem is the textfield in the row=9 change too. The same happen with row=1 and row=10, etc. I really don't get why.

The code of IBAction of the stepper in the tableviewcontroller:

- (IBAction)sumaRestaNormal:(id)sender {
// Get the cell in which the button was pressed
celdaTabacoNormal *cell = (celdaTabacoNormal *)[[[sender superview] superview]superview];
// Get the value of the stepper (it has an outlet in my custom cell
int value = cell.stepperNormal.value;
// Update the text field of the cell with the new value (also has an outlet in the custom cell)
cell.txtNormal.text = [NSString stringWithFormat:@"%d",value];}

The UITextFields are properties in the customcell.h:

@property (weak, nonatomic) IBOutlet UITextField *txtNormal;

In the cellForRowAtIndexPath I have the next:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    celdaTabacoNormal *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if(!cell){
        cell = [[celdaTabacoNormal alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.referenciaTabacoN.text = [arrayRefTabaco objectAtIndex:indexPath.row];
    NSString * marcaNombre = [NSString stringWithFormat:@"%@ - %@", [arrayMarcaTabaco objectAtIndex:indexPath.row], [arrayNomTabaco objectAtIndex:indexPath.row]];
    cell.nombreProductoN.text = marcaNombre;

    return cell;
}

Why the UITextFields from row 0 and row 9, etc change at same time...!

Here I show how the cells are:

http://www.imagesup.net/?di=5140982874911

The problem comes from the reuse of cells. How should I do for controlling this type of cells with steppers and textfields?

jopyesk
  • 111
  • 1
  • 5
  • 1
    Your problem is that cells are re-used and your stepper handler is acting on the data that is in the cell, rather than your data model. You need to create a connection between your custom cell and the data model and ensure that you perform operations on that data, not the cell itself. – Paulw11 Sep 03 '14 at 12:16
  • Ok thank you Paul, the problem comes from the ReusableCells. How can I control the problem of textfield changing from a cell reused (Ej: I change textfield in row=0, then the textfield in row=9 change too and I don't want to, the textfield in row=9 has to change when the stepper in row=9 is used). – jopyesk Sep 04 '14 at 07:49

0 Answers0