Best approach for editing text fields in a table view cell

后端 未结 3 1501
暗喜
暗喜 2021-01-03 03:05

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 UITextF

3条回答
  •  春和景丽
    2021-01-03 03:40

    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];
      }
      

提交回复
热议问题