How do I get the value of UITextField text instance property and update UITableView row whenever the user edits the UITextField text property?

0

How would I update a table view row as as a text field text changes? It is difficult to create a string from the arguments in textField(:shouldChangeCharactersIn:replacementString:) so that the string I create would be equal to the string value of the text property of the text field after the textField(:shouldChangeCharactersIn:replacementString:) occurs. Is there a way to use the UITextInput protocol of UITextField? I haven't found anything on stackoverflow about using the UITextInput protocol of UITextField except a question in 2012 using Objective-C that asks why he's getting a crash.

How would I update a table view row as as a text field text changes?

Where is the TextField ? Outside of the table ?

Why don't you call reload (whole or some cells) when TextField changes ?

Please show code so that we better understand the case.

Here's my code below. The textfield is outside of the table view. I do use reloadRows. I call it from textField(:charactersShouldChangeIn::). It reloads the row so that the table view cell in the row shows the current state of text field's text property before the text property is updated with the change the user made.

self.temporaryMessageBeingEdited?.subject is type String, as you must realize. This temporaryMessageBeingEdited object is in the array the table view uses at the same chronological position as the table view row that is reloaded as must be obvious to you.

Neither the textDidChange(:) nor the textWillChange(:) ever fires, though the other two callback methods of the UITextInputDelegate do fire when I test the code by typing into the text field during run time.

// MARK: - UITextFieldDelegate

extension MessagesViewController: UITextFieldDelegate {
   
  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
     
    print("#", #function)
     
    guard string != "\n" else {
      return false
    }
     
    self.temporaryMessageBeingEdited?.subject = self.textFieldSubject.text
     
    if let guardedIndexPathToReload = self.indexPathOfMessageBeingEdited {
      self.tableView.reloadRows(at: [guardedIndexPathToReload], with: .none)
    }
     
    return true
  }
   
}

// MARK: - UITextInputDelegate

extension MessagesViewController: UITextInputDelegate {
   
  func selectionWillChange(_ textInput: UITextInput?) {
    print("#", #function)
  }
   
  func selectionDidChange(_ textInput: UITextInput?) {
    print("#", #function)
  }
   
  func textWillChange(_ textInput: UITextInput?) {
    print("#", #function)
  }
   
  func textDidChange(_ textInput: UITextInput?) {
     print("#", #function)
  }
    

    }
     
  }
   
}

Why don't you connect from storyboard the textField (which is outside table) to an IBAction on editingTextField event ?

@IBAction func editingTestField(_ sender: UITextField) { 
   update cell here
  }
How do I get the value of UITextField text instance property and update UITableView row whenever the user edits the UITextField text property?
 
 
Q