Прежде всего вам нужно сделать делегат для ячейки и прослушивать, когда текст изменяется и когда редактирование заканчивается, также вам нужно держать индексный путь ячейки в ячейке.
скажем:
protocol CustomTableViewCellDelegate: NSObjectProtocol {
func customTableViewCellDidBeginEditing(at indexPath: IndexPath, didFinish: Bool)
}
и в клетке это будет, как вы знаете:
weak var delegate: CustomTableViewCellDelegate?
var cellIndexPath: IndexPath!
При снятии очереди с ячейки на контроллере представления:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell.cellIndexPath = indexPath
cell.delegate = self
return cell
}
Когда текст начинает меняться, вам нужно вызвать делегата:
self.delegate?.customTableViewCellDidBeginEditing(at: self.cellIndexPath, didFinish: false)
в случае ячейки с текстовым представлением вы хотите вызвать ее, когда это включено:
func textViewDidChange(_ textView: UITextView) {
self.delegate?.customTableViewCellDidBeginEditing(at: self.cellIndexPath, didFinish: false)
}
func textViewDidBeginEditing(_ textView: UITextView) {
self.delegate?.customTableViewCellDidBeginEditing(at: self.cellIndexPath, didFinish: false)
}
И когда текст закончится, вам нужно вызвать делегата:
self.delegate?.customTableViewCellDidBeginEditing(at: self.cellIndexPath, didFinish: true)
в случае ячейки с текстовым представлением, которую вы хотите вызвать, когда это включено:
func textViewDidEndEditing(_ textView: UITextView) {
self.delegate?.customTableViewCellDidBeginEditing(at: self.cellIndexPath, didFinish: true)
}
На стороне контроллера View:
extension ViewController: CustomTableViewCellDelegate {
func customTableViewCellDidBeginEditing(at indexPath: IndexPath, didFinish: Bool) {
// Let assume the Keyboard height is 260 or you can listen to the keyboardWillShowNotification and get the keyboardFrame.cgRectValue.height
let keyboardHeight = 260
self.tableView.contentInset.bottom = didFinish ? keyboardHeight : 0
self.tableView.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.bottom, animated: true)
}
}
Наслаждайтесь