Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's a much simpler approach. In <code>didSelectRowAtIndexPath</code> store a reference to the current cell's UITextField. Then you can set it as first responder, deal with edits, etc.</p> <p>When you tap on a new cell to select it, the delegate first receives a <code>didDeselectRowAtIndexPath</code>. Here you can use your UITextField reference can resign first responder, save any necessary state, etc. Then <code>didDeselectRowAtIndexPath</code> is called on the new cell and you're good to go. So something like the following:</p> <p><strong>Edit: added additional code</strong></p> <pre><code>// Code additions to Xcode's Master-Detail application template // Use dynamic prototypes for table cells, custom cell class follows: // MyCell.h // Property is auto-synthesized, so there is nothing to see in MyCell.m @interface MyCell : UITableViewCell @property (nonatomic, weak) IBOutlet UITextField *tf; @end // MasterViewController.m @interface MasterViewController () { NSMutableArray *_objects; } @property (nonatomic, weak) UITextField *selectedField; @end // this method is called when a cell is DE-selected - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [self.selectedField resignFirstResponder]; // save state, etc if necessary } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { MyCell * cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath]; self.selectedField = cell.tf; // cell.tf is a UITextField property defined on MyCell [self.selectedField becomeFirstResponder]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload