what I have:
a UITableView with a cell. This cell has a UITextView as a subview. All the delegates and data sources are set correctly. The cell height is obtained correctly from heightForRow etc. The user enters text into the textView.
what I'm trying to do:
I want the cell to update its height as the user types WITHOUT resigning the textView as first responder. Many posts on stackOverflow say that an empty beginUpdates/endUpdates works for this.
What I tried:
1- I called beginUpdates and endUpdates (for the tableView) inside textViewDidChange. Nothing happened. (note: all the delegates are set correctly).
2- I even called beginUpdates and endUpdates inside textViewDidEndEditing just to make sure (to see if it works after the textView resigns as first responder). It works (the cell resizes when the textView resigns as firstResponder).
3- I then tried reloadData inside textViewDidChange. It works, except it resigns the textView as first responder (keyboard goes away on every character typed).
4- I then tried reloadData and then directly telling the textView to becomeFirstResponder again (to keep the keyboard up). It works, but this leads to many problems with cursor position and autocorrect and backspace in the textView.
5- I then tried reloadData, and then returning NO in textViewShouldEndEditing when I wanted the keyboard to stay up, but for some reason, the cell would not update its height.
6- Finally, I tried reloadRowsAtIndexPaths etc and then returning NO in textViewShouldEndEditing, but it gave me an error saying it can't remove an object which refuses to resignFirstResponder.
I also tried methods like reloadInputViews and setNeedsDisplay and setNeedsLayout etc inside textViewDidChange, but nothing happens.
My question:
It seems to me that #2 suggests that a cell cannot update its height while one of its subviews is a firstResponder. Is that the case? Is there any documentation on this (I couldn't find any)?
Why is beginUpdates/EndUpdates not working? Are there known or common cases where it doesn't work? Could there be something wrong with the way I'm setting things up? But the resizing works perfectly with #3 and #4 (indicating that all my delegate methods etc are set correctly).
Sorry for the very long question, but can anyone give any ideas besides the ones I posted? I have a feeling that it's some VERY silly and obvious thing that I'm missing (as usual)...
btw, I'm using both iOS simulator and an actual iPhone with 5.1
It works perfectly in my little demo app. Note that the code gets the textView delegate message, sets an ivar height which is applied to every cell, then calls begin/end - the cells all get bigger on each character press:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"Asked for Height");
return height;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog(@"Text");
height += 10;
[self.tableView beginUpdates];
[self.tableView endUpdates];
return YES;
}