I am using iOS 8 self-sizing cells (tableView.rowHeight = UITableViewAutomaticDimension
and tableView:estimatedHeightForRowAtIndexPath:
) combination. It works great until I start typing something inside a cell. When I'm typing, I'm calling beginUpdates
/endUpdates
pair to resize my cell (text view grows as I type and shrinks as I delete) but each call results in a bad jump to top of the table view. If I remove the beginUpdates
/endUpdates
pair then it doesn't jump but my cell doesn't resize as I type.
Here is a demonstration of the issue:
How can I get my cell to resize correctly as I type while not jumping to the top of the table view? I am only targeting iOS >= 8 so I don't need any kind of iOS 7 compatibility.
I was implementing the exactly the same thing for chat app and getting the same issue as you are getting now. This thing helped me out. Let me know if this works for you too.
UIView.setAnimationsEnabled(false) tableView.beginUpdates() cell.textView.scrollRangeToVisible(NSMakeRange(cell.textView.text.characters.count-1, 0)) tableView.endUpdates() UIView.setAnimationsEnabled(true) tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: false)
I found solution from Manoj Aher to work fine - text doesn't jump at all. But in my case I had to extend for situations when user might type much more text (so that the table cell is bigger than the visible part of the table) and then returns to edit this text somewhere at the beginning or in the middle. So I had to scroll the table cell to Top or Middle depending on where the user is typing to keep the typing position visible.
First, remember the indexPath for the cell in any convenient way. For example:
var cellIndexChosenForEdition: NSIndexPath ... func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { cellIndexChosenForEdition = indexPath ... }
In shouldChangeTextInRange or any other method which is called when user types (shown here method will be called when your view controller conforms to UITextViewDelegate protocol):
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { ... UIView.setAnimationsEnabled(false) MyTableView.beginUpdates() MyTableView.endUpdates() UIView.setAnimationsEnabled(true) if let textStartPosition: UITextPosition = textView.selectedTextRange?.start { let cursorPosition = textView.offsetFromPosition(textView.beginningOfDocument, toPosition: textStartPosition) if textView.text.characters.count - cursorPosition < 170 { table_create_issue.scrollToRowAtIndexPath(cellIndexChosenForEdition, atScrollPosition: .Bottom, animated: false) } else if cursorPosition > 200 { table_create_issue.scrollToRowAtIndexPath(cellIndexChosenForEdition, atScrollPosition: .Middle, animated: false) } else { table_create_issue.scrollToRowAtIndexPath(cellIndexChosenForEdition, atScrollPosition: .Top, animated: false) } } ... }
*constants 200 and 170 should be changed to fit your concrete situation
**I didn't use scrollRangeToVisible because my textView height is always equal to cell height and never scrolls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With