Нет сброса значений textField
в вашей текущей реализации cellForRowAt
.Когда пользователь вводит текст в одну из ячеек, он остается там, и поскольку ячейки продолжают использоваться повторно, создается впечатление, что текст просто появляется в случайном порядке.
Введенные пользователем значения должны где-то храниться.Например, есть словарь var userInput = [Int: String]()
.Добавьте UITextFieldDelegate
протокол к вашему контроллеру и выполните следующее:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newText = (textField.text as? NSString)?.replacingCharacters(in: range, with: string)
userInput[textField.tag] = newText // store entered text
return true
}
Затем просто обновите cellForRowAt
этим:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
cell.id = indexPath.item
cell.tag = indexPath.item
cell.n.tag = indexPath.item
// update cell's textfield text
cell.textField.delegate = self
cell.textField.tag = indexPath.item
cell.textField.text = userInput[indexPath.item]
return cell
}