Correct the spelling errors in a UITableView

 

Tap on a cell to scroll up a keyboard. Correct the spelling and then press the return key.

Source code in Correct.zip

  1. Class AppDelegate: unchanged.
  2. Class TableViewController

Create the project

Create the TableViewController as in States.

tableView:didDeselectRowAtIndexPath:

When a cell is selected, the tableView(_:didDeselectRowAtIndexPath:) method of the table view delegate is called. This method adds a UITextField to the cell, temporarily covering the cell’s textLabel. The position, content, and color (fore and background) of the UITextField exactly match those of the textLabel. When you press return, the UITextField’s text will be copied into the cell and the UITextField will be destroyed.

Minor details. A UILabel displays its text centered vertically. A UITextField displays its text upper-left justified. (Try to find this in the documentation!) That’s why we needed the variable dy.

We change the textLabel’s text to a blank just in case the background color is clear.

The methods

Three methods of the data source provide the data. Our data source is the UITableViewController.

  1. numberOfSectionsInTableView(_:)
  2. tableView(_:numberOfRowsInSection:)
  3. tableView(_:cellForRowAtIndexPath:) returns a UITableViewCell.

Things to try

  1. Would it be simpler to invent a subclass of UITableViewCell that has a UITextField in place of the UILabel textLabel? Then every cell would contain a UITextField all the time.