0

I used a tableView as a registration screen and I need to enforce some length limitation to every row. I used the function below but it lets all the fields have the same constraint. Does anyone know how to handle it?

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let maxLength = 12
    let currentString: NSString = textField.text! as NSString
    let newString: NSString =
        currentString.replacingCharacters(in: range, with: string) as NSString
    return newString.length <= maxLength
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

Since your text field delegate methods are in the cell, the cell needs to know what length to enforce for its text field.

Your view controller needs to provide that length to each cell in cellForRowAt based on the current index path.

Add a textLength property to your cell class and then set that value as needed in cellForRowAt.

Then your shouldChangeCharactersIn in the cell class can use that textLength property instead of the hardcoded 12 you have now.

rmaddy
  • 314,917
  • 42
  • 532
  • 579