UITableView jumping to top on endUpdates while typing inside a cell on iOS 8 auto height [duplicate] UITableView jumping to top on endUpdates while typing inside a cell on iOS 8 auto height [duplicate] ios ios

UITableView jumping to top on endUpdates while typing inside a cell on iOS 8 auto height [duplicate]


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.


I'm having the same problem, and have been able to minimize (but not eliminate) the jumping by only doing the resize/update notification when the intrinsic size of the content has actually changed, e.g.:

Swift

var textViewHeight: CGFloat = 0.0  // Set in viewWillAppear as below...func textViewDidChange(textView: UITextView) {    let newHeight = textView.intrinsicContentSize().height    if textViewHeight != newHeight{        textViewHeight = newHeight        tableView.beginUpdates()        tableView.endUpdates()    }}

Objective-C

 CGFloat textViewHeight = 0.0;  // Set in viewWillAppear as below ... (void)textViewDidChange:(UITextView *)textView {    CGFloat newHeight = [textView intrinsicContentSize].height;    if (textViewHeight != newHeight){        textViewHeight = newHeight        [tableView beginUpdates];        [tableView endUpdates];    }}


matomo