Note that there are some explanatory texts on larger screens.

plurals
  1. POincrease uitableviewcell height simultaneously increasing the inner UITextView
    primarykey
    data
    text
    <p>I create a <code>UITableView</code> with different types of <code>UITableViewCell</code> depending on the type of content to display. One of this is a <code>UITableViewCell</code> with inside an <code>UITextView</code> programmatically created in this way:</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... if([current_field.tipo_campo isEqualToString:@"text_area"]) { NSString *string = current_field.valore; CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap]; CGFloat height = ([string isEqualToString:@""]) ? 30.0f : stringSize.height+10; UITextView *textView=[[UITextView alloc] initWithFrame:CGRectMake(5, 5, 290, height)]; textView.font = [UIFont systemFontOfSize:15.0]; textView.text = string; textView.autoresizingMask = UIViewAutoresizingFlexibleWidth; textView.textColor=[UIColor blackColor]; textView.delegate = self; textView.tag = indexPath.section; [cell.contentView addSubview:textView]; [textView release]; return cell; } ... } </code></pre> <p>Since the text view is editable the cell that contains it should change its height to correctly fit the text view sizes. Initially I did this by resizing the <code>UITextView</code> inside the method <code>textViewDidChange</code>:, in this way:</p> <pre><code>- (void)textViewDidChange:(UITextView *)textView { NSInteger index = textView.tag; Field* field = (Field*)[[self sortFields] objectAtIndex:index]; field.valore = textView.text; [self.tableView beginUpdates]; CGRect frame = textView.frame; frame.size.height = textView.contentSize.height; textView.frame = frame; newHeight = textView.contentSize.height; [self.tableView endUpdates]; } </code></pre> <p>I save the new height of text view in a variable and then when <code>tableView:heightForRowAtIndexPath</code>: method is called, I resize the cell in this way:</p> <pre><code>- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { ... if ([current_field.tipo_campo isEqualToString:@"text_area"]) { return newHeight +10.0f; } else return 44.0f; ... } </code></pre> <p>In this way both are resized but is not done in sync, ie first the <code>TextView</code> is resized and then it is resized the height of the cell, so for an instant the user see that the text view is larger than the cell. How can I fix this bad behavior?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload