I have a textView, in a custom cell, where when I begin editing I would like buttons to appear in the cell. To do this I have created a variable 'selectedIndexPathRow' to which I assign the indexPath.row of the textView in the 'textViewDidBeginEditing' function.
func textViewDidBeginEditing(_ textView: UITextView) {
let touchPosition:CGPoint = textView.convert(CGPoint.zero, to: self.createStoryTableView)
let indexPath = self.createStoryTableView.indexPathForRow(at: touchPosition)
self.selectedIndexPathRow = indexPath?.row ?? 0
.....
After which in my cellForRowAt I can show or hide the buttons according to whether the indexPathRow matches the variable. As follows:
if indexPath.row == selectedIndexPathRow {
cell.inputTextView.isEditable = true
cell.createBtnStack.isHidden = false
} else {
cell.createBtnStack.isHidden = true
}
The problem is that the buttons only appear after the editing has completed (on pressing enter or clicking outside of textView) and not at the same as when the editing begins (or when the textView is clicked).
To rectify this I have tried the following:
putting in tableview.reloaddata() into the 'didBeginEditing'. This produces the buttons synchronously but causes the textview to freeze.
using the 'textViewShouldBeginEditing' function as follows to try to pre-set the selectIndexPathRow variable:
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
let touchPosition:CGPoint = textView.convert(CGPoint.zero, to: self.createStoryTableView)
let indexPath = self.createStoryTableView.indexPathForRow(at: touchPosition)
self.selectedIndexPathRow = indexPath?.row ?? 0
return true }
But the result of this is not consistent - sometimes the textview can be edited directly after clicking with no buttons showing up, sometimes the buttons show up but you have to click again to edit the textView.
Tell me if you have any suggestions on how to solve this.