Сначала вам нужно создать способ связи между вашей ячейкой и контроллером представления.
Вы можете использовать шаблон делегата или обратные вызовы для этого.
Например:
final class TextFieldCell: UITableViewCell {
// MARK: - IBOutlets
@IBOutlet weak var textField: UITextField!
// MARK: - Local variables
var callback: ((_ text: String) -> Void)?
// MARK: - Lyfecycle
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}
}
Также не забудьте перезвонить нам:
extension TextFieldCell: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
callback?(textField.text!)
return true
}
}
Отлично! Теперь мы отправляем нашу строку из ячейки в контроллер!
Пример кода для вашего контроллера представления (упрощенная версия):
class ViewController: UIViewController {
// MARK: - IBOutlets
@IBOutlet weak var tableView: UITableView!
// MARK: - Local variables
var titles = ["Hello", "world"]
}
// MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let textFieldCell = tableView.dequeueReusableCell(withIdentifier: "textFieldCell", for: indexPath) as! TextFieldCell
textFieldCell.textField.placeholder = titles[indexPath.row]
textFieldCell.callback = { [weak self] newTitle in // don't forget about memory leaks
guard let `self` = self else { return }
// calculating index path for new row
let newIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)
// appending a new row in table view
self.titles.append(newTitle)
self.tableView.insertRows(at: [newIndexPath], with: UITableView.RowAnimation.automatic)
}
return textFieldCell
}
}