Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach for editing text fields in a table view cell

In my app I've got a number of views that need to be in-place editable. I've got the tableviewcells setup to include a UITextField which is enabled, and the text can be changed and this is fine, it works.

My question is what is the best approach to keeping track of these? Should I :

  • Keep an iVar of each textfield. If so then
    • How do I make sure each cell has the correct field if it's only instantiated once?
  • Have a textfield added when the cell is created and simply set its value when configuring the cell. If so then
    • How do I keep track of the values? If the cell went out of view and then back, its values would be reset to its original value and not the edited value.

What approach do you generally tend to use for this scenario? Especially if there are a large number of cells that you're dealing with.

like image 579
PaReeOhNos Avatar asked Jan 16 '13 04:01

PaReeOhNos


3 Answers

  1. Store the values for all of your text fields in a mutable array in your data model.

  2. In tableView:willDisplayCell:forRowAtIndexPath: set your text field's value, using the value from your array based on the index path.

  3. When a text field ends editing, store the value back in the array immediately. There is already a protocol for this (so no need to write your own), but it is a little trickier than normal because you are not handed the indexPath and need to find the one associated with your text field:

    - (void) textFieldDidEndEditing:(UITextField *)textField {
        CGRect location = [self convertRect:textField.frame toView:self.tableView];
        NSIndexPath *indexPath = [[self.tableView indexPathsForRowsInRect:location] objectAtIndex:0];
    
        // Save the contents of the text field into your array:
        [yourArray replaceObjectAtIndex:indexPath.row withObject:textField.text];
    }
    
like image 64
lnafziger Avatar answered Oct 16 '22 16:10

lnafziger


  • Subclass UITableViewCell and add a UITextField in it's init method.

  • create a protocol for the custom cell class. when UITextField begin editing or end editing call the delegate's method.

  • in controller, implement the custom cell's protocol and set the indexpath row value to cell's tag in tableView:cellForRowAtIndexPath:

so every time you edit UITextField, you can know in controller which row is editing (via cell's tag)

like image 20
jessex Avatar answered Oct 16 '22 18:10

jessex


The best way is to follow MVC and implement your model, which in your case is the textfield values. What you do is store those values in an array, and in your cellForIndexPath method you reference the correct value in the array. For example, the first row of the table (indexPath.row == 0) would retrieve the first element in the array. Changes in the textField would be recorded on the corresponding element in the array.

like image 21
sosborn Avatar answered Oct 16 '22 16:10

sosborn