title: How to make textField in TableViewCell editable when edit button of NavigationBar is pressed?
body: <p>Developing an iOS application with Xcode ver 9.2, Swift.</p>
  
  <p>When the edit button on the top right of the NavigationBar is pressed, how to change the textField in the TableViewCell to make it editable?
  To prevent the TextField from being edited in the initial display, I set textField.isEnabled = false with awakeFromNib() in the TableViewCell.swift.
  When the edit button is pressed, I want to set it to true so that I can edit the TextField.
  Could you tell me how?</p>
  
  <p>Relationship between object placement and code (in parentheses) is below.</p>
  
  <p>NavigationController - TableViewController (TableViewController.swift) - TableViewCell (TableViewCell.swift) - TextField</p>
  
  <p>Here is the code.</p>
  
  <p>TableViewController.swift</p>
  
  <pre><code>import UIKit
  
  class TableViewController: UITableViewController, TableViewCellDelegate {
  
      @IBOutlet var ttableView: UITableView!
  
      var array:[String] = ["aaa", "bbb", "ccc", "ddd", "eee"]
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // Uncomment the following line to preserve selection between presentations
          // self.clearsSelectionOnViewWillAppear = false
  
          // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
          self.navigationItem.rightBarButtonItem = self.editButtonItem
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      override func setEditing(_ editing: Bool, animated: Bool) {
          super.setEditing(editing, animated: animated)
      }
  
      // MARK: - Table view data source
  
      override func numberOfSections(in tableView: UITableView) -&gt; Int {
          // #warning Incomplete implementation, return the number of sections
          return 1
      }
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          // #warning Incomplete implementation, return the number of rows
          return array.count
      }
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "inputCell", for: indexPath) as! TableViewCell
          cell.textField.text = array[indexPath.row]
          cell.delegate = self
          return cell
      }
  
      func textFieldDidEndEditing(cell: TableViewCell, value: String) -&gt; () {
          let path = tableView.indexPathForRow(at: cell.convert(cell.bounds.origin, to: tableView))
          array[(path?.row)!] = value
      }
  
      override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
          if (editingStyle == UITableViewCellEditingStyle.delete) {
              array.remove(at: indexPath.row)
              tableView.deleteRows(at: [indexPath], with: .fade)
          }
      }
  
      override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
  
          let cell = tableView.cellForRow(at: sourceIndexPath) as! TableViewCell
          let moveData = cell.textField.text
          array.remove(at: sourceIndexPath.row)
          array.insert(moveData!, at: destinationIndexPath.row)
      }
  
  }
  </code></pre>
  
  <p>TableViewCell.swift</p>
  
  <pre><code>import UIKit
  
  protocol TableViewCellDelegate {
      func textFieldDidEndEditing(cell: TableViewCell, value: String) -&gt; ()
  }
  
  class TableViewCell: UITableViewCell, UITextFieldDelegate {
  
      var delegate: TableViewCellDelegate! = nil
  
      @IBOutlet weak var textField: UITextField!
  
      override func awakeFromNib() {
          super.awakeFromNib()
          textField.delegate = self
          textField.returnKeyType = .done
  
          // To prevent the TextField from being edited in the initial display 
          textField.isEnabled = false
      }
  
      override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
  
          // Configure the view for the selected state
      }
  
      func textFieldDidEndEditing(_ textField: UITextField) {
          self.delegate.textFieldDidEndEditing(cell: self, value: textField.text!)
      }
  
      func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
          textField.resignFirstResponder()
          return true
      }
  
  }
  </code></pre>
  
  <p>I added the following from the first time question and answers.</p>
  
  <p><a href="https://i.stack.imgur.com/Suxeh.png" rel="nofollow noreferrer">Editing screen shot: after edit button is pressed</a></p>
  
  <p>If there are many elements of the array, the cells will be outside the screen, but I want to make all textField editable as well.</p>
  
  <pre><code>var array:[String] = ["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp", "qqq", "rrr", "sss", "ttt"]
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/qNYfg.png" rel="nofollow noreferrer">Editing screen shot for many elements</a></p>
  
  <p><strong>Finally resolved code</strong></p>
  
  <p>TableViewController.swift</p>
  
  <pre><code>import UIKit
  
  class TableViewController: UITableViewController, TableViewCellDelegate {
  
      @IBOutlet var ttableView: UITableView!
  
  //    var array:[String] = ["aaa", "bbb", "ccc", "ddd", "eee"]
  
      var array:[String] = ["aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll", "mmm", "nnn", "ooo", "ppp", "qqq", "rrr", "sss", "ttt"]
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          // Uncomment the following line to preserve selection between presentations
          // self.clearsSelectionOnViewWillAppear = false
  
          // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
          // self.navigationItem.rightBarButtonItem = self.editButtonItem
          navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(rightBarButtonItemTapped))
      }
  
      // handle tap by button...
      @objc func rightBarButtonItemTapped(_ sender: UIBarButtonItem) {
          ttableView.setEditing(!ttableView.isEditing, animated: true)
          navigationItem.rightBarButtonItem?.title = ttableView.isEditing ? "Done" : "Edit"
          navigationItem.rightBarButtonItem?.style = ttableView.isEditing ? .done : .plain
          ttableView.visibleCells.forEach { cell in
              guard let cell = cell as? TableViewCell else { return }
              cell.textField.isEnabled = ttableView.isEditing
          }
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      // MARK: - Table view data source
  
      override func numberOfSections(in tableView: UITableView) -&gt; Int {
          // #warning Incomplete implementation, return the number of sections
          return 1
      }
  
      override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          // #warning Incomplete implementation, return the number of rows
          return array.count
      }
  
      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "inputCell", for: indexPath) as! TableViewCell
          cell.textField.text = array[indexPath.row]
          cell.textField.isEnabled = tableView.isEditing
          cell.delegate = self
          return cell
      }
  
      func textFieldDidEndEditing(cell: TableViewCell, value: String) -&gt; () {
          let path = tableView.indexPathForRow(at: cell.convert(cell.bounds.origin, to: tableView))
          array[(path?.row)!] = value
      }
  
      override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
          if (editingStyle == UITableViewCellEditingStyle.delete) {
              array.remove(at: indexPath.row)
              tableView.deleteRows(at: [indexPath], with: .fade)
          }
      }
  
      override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -&gt; UITableViewCellEditingStyle {
          if tableView.isEditing {
              return UITableViewCellEditingStyle.delete
          } else {
              return UITableViewCellEditingStyle.none
          }
      }
  
      override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
          let cell = tableView.cellForRow(at: sourceIndexPath) as! TableViewCell
          let moveData = cell.textField.text
          array.remove(at: sourceIndexPath.row)
          array.insert(moveData!, at: destinationIndexPath.row)
      }
  
  }
  </code></pre>
  
  <p>TableViewCell.swift</p>
  
  <pre><code>import UIKit
  
  protocol TableViewCellDelegate {
      func textFieldDidEndEditing(cell: TableViewCell, value: String) -&gt; ()
  }
  
  class TableViewCell: UITableViewCell, UITextFieldDelegate {
  
      var delegate: TableViewCellDelegate! = nil
  
      @IBOutlet weak var textField: UITextField!
  
      override func awakeFromNib() {
          super.awakeFromNib()
          textField.delegate = self
          textField.returnKeyType = .done
          //textField.isEnabled = false
      }
  
      override func setSelected(_ selected: Bool, animated: Bool) {
          super.setSelected(selected, animated: animated)
  
          // Configure the view for the selected state
      }
  
      func textFieldDidEndEditing(_ textField: UITextField) {
          self.delegate.textFieldDidEndEditing(cell: self, value: textField.text!)
      }
  
      func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
          textField.resignFirstResponder()
          return true
      }
  
  }
  </code></pre>