iOS – How to change the height of UITableView to adapt to its dynamic content?

I have a UITableView, which contains a cell, and the cell contains a TTTextEditor (a Three20 control), all intents and purposes are UITextView. I am using TTTextEditor , So the user can enter a dynamic amount of text, and when the control is resized, I am using the delegate method to increase the height of the containing cell accordingly. So far it’s pretty good.

When After I finished editing, when the cell is now higher, the table no longer scrolls enough to cover the content. I assume I need to adjust the height of the table to compensate for the increase in cell height, but no matter what I try seems to have no effect. From reading , I am trying this:

CGRect frame = [self.tableView frame];
CGSize contentSize = self.tableView.contentSize;

[self.tableView setFrame:CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
contentSize.height)];

< p> But it doesn’t work-the frame height does increase, but it doesn’t increase to the correct height, and in any case, the table won’t scroll more than it originally was. I also tried the sizeToFit method on the tableView without success.

Does anyone have an in-depth understanding of how to increase the scrollable height of the tableview?

You need to set the contentSize instead of the frame. Calculate what the size should be, and then set it. It’s possible You need to do some calculations. If you are processing text, then you may want to see the method added to NSString called sizeWithFont:constrainedToSize:lineBreakMode:. On the other hand, your frame should always be the size of the visible area, and Not a scrollable area.

Edit: Please note that for the initial layout that has not changed at that time, you may want to use the standard table delegation mechanism-(CGFloat)tableView: (UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath, but when it needs to change dynamically like this, I think it’s worth trying to adjust the content size.

I have a UITableView, which contains a cell , And the cell contains a TTTextEditor (a Three20 control), all intents and purposes are UITextView. I am using TTTextEditor, so the user can enter a dynamic amount of text, and when the control is resized, I am using a delegate Method to increase the height of the containing cell accordingly. So far so good.

When I finished editing, when the cell is now taller, the table no longer scrolls enough to cover the content . I assume that I need to adjust the height of the table to compensate for the increase in cell height, but no matter what I try seems to have no effect. From reading, I am trying this:

CGRect frame = [self.tableView frame];
CGSize contentSize = self.tableView.contentSize;

[self.tableView setFrame:CGRectMake(frame.origin.x,
frame. origin.y,
frame.size.width,
contentSize.height)];

But it doesn’t work-the frame height does increase, but it doesn’t increase to the correct height, and in any case, the table won’t scroll more than it was originally. I also I tried the sizeToFit method on the tableView but it didn’t work.

Can anyone know how to increase the scrollable height of the tableview?

You need to set the contentSize instead of the frame. Calculate what the size should be, and then set it. This may require you to do some calculations. If you are dealing with text, Then you may want to see the method added to NSString, named sizeWithFont:constrainedToSize:lineBreakMode:. On the other hand, your frame should always be the size of the viewable area, not the scrollable area.

Edit: Please note that for the initial layout that has not changed at that time, you may want to use the standard table delegation mechanism-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath, but when you need something like this The situation changes dynamically, I think it is worthwhile to adjust the content size.

Leave a Comment

Your email address will not be published.