-1

I have a TableView (named myTableView) that contains prototype cells (named: TableCell) inside a ViewController (not TableViewController, named: ViewController).

Each cell has 4 different textfields that display data from different NSMutableArrays (declared in ViewController.h and named: Legajos, Nombres, Cbus, Sueldos, Contribuciones), so for example the cell at row 0 will display the objects at index 0 from Legajos, Nombres, etc... All of this is working, I am even able to add and delete rows with no problem.

Now my problem. I have textfields (declared TableCell.h) that display text from NSMutableArrays (declared in ViewController.h), but I was only able to make it a "one way trip", that is to say, the textfields only show text from the NSMutableArrays but I also need them to modify the NSMutableArray so I can save that array and then load it.

The NSMutableArrays declaration: (ViewController.h)

@property (strong, nonatomic) NSMutableArray *Legajos;
@property (strong, nonatomic) NSMutableArray *Nombres;
@property (strong, nonatomic) NSMutableArray *Cbus;
@property (strong, nonatomic) NSMutableArray *Sueldos;
@property (strong, nonatomic) NSMutableArray *Contribuciones;

Here is how the cells get their text: (ViewController.m)

cell.legajo.text = [Legajos objectAtIndex:indexPath.row];
cell.nombre.text = [Nombres objectAtIndex:indexPath.row];
cell.cbu.text = [Cbus objectAtIndex:indexPath.row];
cell.sueldo.text = [Sueldos objectAtIndex:indexPath.row];
cell.contribucion.text = [Contribuciones objectAtIndex:indexPath.row];

And this are the IBOutlets of the cells: (TableCells.h)

@property (strong, nonatomic) IBOutlet UILabel *legajo;
@property (strong, nonatomic) IBOutlet UITextField *nombre;
@property (strong, nonatomic) IBOutlet UITextField *cbu;
@property (strong, nonatomic) IBOutlet UITextField *sueldo;
@property (strong, nonatomic) IBOutlet UITextField *contribucion;

Sorry if its hard to understand. Thanks for any help given.

Franco Altuna
  • 188
  • 1
  • 3
  • 12
  • When the cell is modified you need to modify the array. You'd do this in the UITextFieldDelegate methods. – Hot Licks Oct 03 '14 at 02:02
  • @HotLicks The thing is I don't know how to modify the NSMutableArray located at the ViewController _from the TableCell_. I know how to modify an array. – Franco Altuna Oct 03 '14 at 02:15
  • The text field delegate should know how to modify it. Simply a matter of passing addressability when the delegate is created. (And note that there's no substantial difference between a TableView in a ViewController vs a TableViewController.) – Hot Licks Oct 03 '14 at 02:24
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hot Licks Oct 03 '14 at 02:26

1 Answers1

0

Easieast way would probably be to add (void) textFieldDidEndEditing:(UITextField *)textField method to your UITextField delegate. You can either assign a tag to each UITextField upon creation so you know which array to edit or I believe NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]];might do the trick.

 (void) textFieldDidEndEditing:(UITextField *)textField {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]]; 

        switch(indexPath.section) 
    {
        case '0':
            // edit object in first array at index indexPath.row
            [firstArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        case '1':
            // edit object in second array at index indexPath.row
            [secondArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        case '2':
            // edit object in third array at index indexPath.row
            [thirdArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        case '3':
            // edit object in fourth array at index indexPath.row
            [fourthArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        case '0':
            // edit object in fifth array at index indexPath.row
            [fifthArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
            break;
        default :
            }

    }

Here is a quick example I've typed up.

TyloBedo
  • 495
  • 2
  • 12