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.
我有一个UITableView,它包含一个单元格,而该单元格又包含一个TTTextEditor(一个Three20控件),所有意图和目的都是UITextView。我正在使用TTTextEditor,因此用户可以输入动态数量的文本,并且当控件调整大小时,我正在使用委托方法来相应地增加包含单元格的高度。到现在为止还挺好。
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)];
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.
但它不起作用 - 框架高度确实增加,但没有增加到正确的高度,并且在任何情况下,桌子不会滚动它原来的更多。我也在tableView上尝试了sizeToFit方法但没有成功。
Does anyone have an insight into how I can get the tableview's scrollable height to increase?
有没有人能够深入了解如何让tableview的可滚动高度增加?
2 个解决方案
#1
5
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.
您需要将contentSize设置为不是框架。计算大小应该是什么,然后设置它。这可能需要您进行一些计算。如果您正在处理文本,那么您可能希望查看添加到NSString中的方法,名为sizeWithFont:constrainedToSize:lineBreakMode:。另一方面,您的框架应始终是可视区域的大小,而不是可滚动区域。
EDIT: 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.
编辑:请注意,对于那时没有改变的初始布局,您可能希望使用标准表委托机制 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath,但是当需要像这种情况动态变化,我认为调整内容大小值得一试。
#2
2
The first thing you need to do is to implement the table view delegate's -tableView:heightForRowAtIndexPath:
method. In this method you specify the height taken by each row in a table. If some rows in your table have a height which is different from others, then you need to implement this.
您需要做的第一件事是实现表视图委托的-tableView:heightForRowAtIndexPath:方法。在此方法中,您可以指定表中每行的高度。如果表中的某些行的高度与其他行不同,则需要实现此操作。
When this method is called for your text editor's row, just find the view's height and return it.
为文本编辑器的行调用此方法时,只需查找视图的高度并将其返回。
The second thing you need to do is to tell the table view to refresh itself whenever the editor's height change. I don't know how TTTextEditor works, but you can check that for a UITextView by implementing its delegate's -textViewDidChange:
method, for example. Once you know that the editor's height has changed just call the table view's -reloadData
method -- that should adjust the table's height appropriately.
您需要做的第二件事是告诉表视图在编辑器的高度变化时刷新自己。我不知道TTTextEditor是如何工作的,但是你可以通过实现其委托的-textViewDidChange:方法来检查UITextView。一旦你知道编辑器的高度已经改变,只需调用表视图的-reloadData方法 - 这应该适当地调整表的高度。
#1
5
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.
您需要将contentSize设置为不是框架。计算大小应该是什么,然后设置它。这可能需要您进行一些计算。如果您正在处理文本,那么您可能希望查看添加到NSString中的方法,名为sizeWithFont:constrainedToSize:lineBreakMode:。另一方面,您的框架应始终是可视区域的大小,而不是可滚动区域。
EDIT: 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.
编辑:请注意,对于那时没有改变的初始布局,您可能希望使用标准表委托机制 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath,但是当需要像这种情况动态变化,我认为调整内容大小值得一试。
#2
2
The first thing you need to do is to implement the table view delegate's -tableView:heightForRowAtIndexPath:
method. In this method you specify the height taken by each row in a table. If some rows in your table have a height which is different from others, then you need to implement this.
您需要做的第一件事是实现表视图委托的-tableView:heightForRowAtIndexPath:方法。在此方法中,您可以指定表中每行的高度。如果表中的某些行的高度与其他行不同,则需要实现此操作。
When this method is called for your text editor's row, just find the view's height and return it.
为文本编辑器的行调用此方法时,只需查找视图的高度并将其返回。
The second thing you need to do is to tell the table view to refresh itself whenever the editor's height change. I don't know how TTTextEditor works, but you can check that for a UITextView by implementing its delegate's -textViewDidChange:
method, for example. Once you know that the editor's height has changed just call the table view's -reloadData
method -- that should adjust the table's height appropriately.
您需要做的第二件事是告诉表视图在编辑器的高度变化时刷新自己。我不知道TTTextEditor是如何工作的,但是你可以通过实现其委托的-textViewDidChange:方法来检查UITextView。一旦你知道编辑器的高度已经改变,只需调用表视图的-reloadData方法 - 这应该适当地调整表的高度。