在iOS中动态调整TableViewCell的大小

时间:2015-08-10 12:47:01

标签: ios objective-c iphone

我有四个UIView放在UITableViewCell上就像 每个单元格包含一个视图。我想在点击特定单元格时动态扩展TableViewCell,并调整后续单元格。 怎么做?

2 个答案:

答案 0 :(得分:1)

您需要致电tableView.beginUpdates(),然后在您要更改的行索引上调用tableView.reloadRowsAtIndexPaths(:withRowAnimation:),并tableView.endUpdates()

您还需要实施tableView(:heightForRowAtIndexPath:)。这应该会回归'标准'大多数行的高度(默认为44.0),然后是展开行的较大值。

答案 1 :(得分: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
相关问题