views:

157

answers:

2

Hi everyone, I have a UITextView in a UITableViewCell contentview and allow the cell to autoresize so that the entered text is fully shown - what I am trying to accomplish is an autoresizing cell like the native iOS4 Contacts app has, when you enter "notes" for contact - i.e. when the contentSize of the textView changes - I call reloadRowsAtIndexPaths and in the delegate's heightForRowAtIndexPath I provide the new height for row - this does the job, however it is not nice and smooth like the contact's app - I am almost sure Apple uses some undocumented trick in that app to make the cell's contentView expand smooth and animated without calling reloadRowsAtIndexPaths. My question is how would you suggest to implement such functionality? I hope I didn't miss any details in explanation.

+1  A: 

Try this code below, it will be help. You don't have to use any reload functions like reloadRowsAtIndexPaths.

// textview delegate

- (void)textViewDidChange:(UITextView *)textView {
    if (contentView.contentSize.height > contentRowHeight) {

    contentRowHeight = contentView.contentSize.height;

    [theTableView beginUpdates];
    [theTableView endUpdates];

    [contentView setFrame:CGRectMake(0, 0, 300.0, contentView.contentSize.height)];
    }
}

// tableview delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height;

    if (indexPath.row == 0)
        height = kTitleRowHeight;
    else 
        height = contentRowHeight;

    return height;
}
Porco Kang
what is contentView in this code? I assume it is textView?
justadreamer
That's property of UITableViewCell. UITextView is added in this property. [cell.contentView addSubview:textView];
Porco Kang
I'm using this solution in my app and it is working 100% on the iPhone, but on the iPad it is not: when the user types more than one line of text, the `UITableViewCell` gets taller as it is supposed to, but the `UITextView` actually moves *up*, so that the first line of text is above the top of the `UITableViewCell`--and hidden. Any ideas of how to solve this on the iPad?
Jason
PS I have posted my code for this at http://stackoverflow.com/questions/4015557/uitextview-in-a-uitableviewcell-smooth-auto-resize-works-on-iphone-but-not-ipad
Jason
A: 

Porco Kangs answer works for me, except that the keyboard slides down and up again, during the animation. (this is when I when the UITextField is active). Any solutions for this?

Olof
I don't use his solution - I use my own, which does the job by calling reloadRowsAtIndexPaths once the contentSize of the textView does not fit in the cell's size - the keyboard is not dismissed in this case.
justadreamer
This is also happening for me, any suggestions?
Jason