在iOS中动态调整TableViewCell的大小

在iOS中动态调整TableViewCell的大小,ios,objective-c,iphone,Ios,Objective C,Iphone,我有四个UIView,放在UITableViewCell上,就像 每个单元格包含一个视图。我想在单击相应的单元格时动态展开TableViewCell,并调整下面的单元格。 如何操作?您需要调用tableView.BeginUpdate(),然后调用tableView.reloadRowsAtIndexPaths(:withRowAnimation:),对要更改的行索引和tableView.EndUpdate() 您还需要实现表格视图(:heightforrowatinexpath:)。这将返回大

我有四个UIView,放在UITableViewCell上,就像 每个单元格包含一个视图。我想在单击相应的单元格时动态展开TableViewCell,并调整下面的单元格。
如何操作?

您需要调用
tableView.BeginUpdate()
,然后调用
tableView.reloadRowsAtIndexPaths(:withRowAnimation:)
,对要更改的行索引和
tableView.EndUpdate()

您还需要实现
表格视图(:heightforrowatinexpath:)
。这将返回大多数行的“标准”高度(默认为44.0),然后为扩展行返回更大的值。

一些示例代码:

            @implementation ViewController {

                NSIndexPath *indexPathToExpand;
            }

            -(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath {
                 if(indexPath == indexPathToExpand)
                     return expandedHeight;
                 else 
                     return defaultHeight;
            }

           -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
                  indexPathToExpand = indexPath;
                  [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }

            @end

你试过什么吗?这里回答了这个问题: