This question already has an answer here:
-
reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling
13 answers
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.
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];
}
}
Are your reseting frame of Table View Cell on text begin editing? If yes, then you can check your variables sometimes due to memory allocation variables deallocate. Use break points and check variable values that can not be change according your requirement.
Auto layout have some drawback associated with it. When you start typing it jump because all other cell are adjusting their size so it will be better if you calculate the dimensions of cell on runtime only.