如何更改UITableView的高度以适合其动态内容? [英] How do I change the height of a UITableView to fit its dynamic content?

查看:47
本文介绍了如何更改UITableView的高度以适合其动态内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,它包含一个单元格,该单元格又​​包含一个TTTextEditor(Three20控件),出于所有目的和目的,它是一个UITextView.我正在使用TTTextEditor,以便用户可以输入动态数量的文本,并且在控件调整大小时,我正在使用委托方法来相应地增加包含单元格的高度.到目前为止一切顺利.

I've a UITableView which contains a cell, which in turn contains a TTTextEditor (a Three20 control) which for all intents and purposes is a UITextView. I'm using the TTTextEditor so the user can enter a dynamic amount of text, and as the control resizes I'm using a delegate method to increase the height of the containing cell accordingly. So far so good.

当我完成编辑后,单元格现在变高了,表格不再滚动到足以覆盖内容的程度.我假设我需要调整桌子的高度以补偿单元格高度的增加,但是我尝试执行的任何操作似乎都没有效果.通过阅读,我正在尝试:

When I've finished editing, with the cell now taller, the table no longer scrolls enough to cover the content. I'm assuming I need to adjust the height of the table to compensate for the increase in the cell height, but whatever I try doesn't seem to have an effect. From reading around, I'm 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)];

但是它不起作用-框架的高度会增加,但不会达到正确的高度,并且在任何情况下,桌子的滚动都不会比其原来的滚动更多.我还在tableView上尝试过sizeToFit方法,但没有成功.

But it doesn't work - the frame height does increase, but not to the correct height, and in any case the table scrolls no more that it did originally. I've also tried the sizeToFit method on the tableView without success.

有人了解我如何增加表格视图的可滚动高度吗?

Does anyone have an insight into how I can get the tableview's scrollable height to increase?

推荐答案

您需要设置 contentSize 而不是 frame .计算应该的大小,然后进行设置.您可能需要进行一些计算.如果要处理文本,则可能需要查看添加到 NSString 中的方法,该方法称为 sizeWithFont:constrainedToSize:lineBreakMode:.另一方面,您的框架应该始终是可视区域的大小,而不是滚动区域.

You need to set the contentSize not the frame. Calculate what the size SHOULD be, and then set it. This may require some calculation on your part. If you're dealing with text, then you may want to look at the method added to NSString called sizeWithFont:constrainedToSize:lineBreakMode:. Your frame, on the other hand, should always be the size of the viewable area, NOT the scrollable area.

请注意,对于当时未更改的初始布局,您可能希望使用-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath ,但是当需要像这种情况那样动态更改时,我认为调整内容大小是值得尝试的.

Note that for the initial layouts that are not changing at that moment, you will probably want to use the standard table delegate mechanism of - (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath, but when that needs to change dynamically like this situation, I think adjusting the content size is worth a try.

这篇关于如何更改UITableView的高度以适合其动态内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆