在方向更改时更改UITableViewCell高度

Pet*_*ich 11 resize orientation uitableview

我有一个UITableView,单元格包含可变高度的UILabels.我能够计算标签需要使用的最小高度sizeWithFont:constrainedToSize:lineBreakMode:,这在首次加载表视图时工作正常.当我旋转表格视图时,单元格变宽(意味着显示内容所需的行数更少).在方向更改动画期间或之前或之后,有什么方法可以让UITableView重新确定单元格的高度?谢谢.

Dav*_*har 13

您的UITableViewController可以实现该tableView:heightForRowAtIndexPath:方法来指定特定行的行高,并且可以查看当前方向([[UIDevice currentDevice] orientation])以确定要返回的高度.

为了确保tableView:heightForRowAtIndexPath:在方向发生变化时再次调用,您可能需要按照此问题中的描述检测方向更改,然后调用[tableView reloadData].

  • 根据这个问题:http://stackoverflow.com/questions/460014/can-you-animate-a-height-change-on-a-uitableviewcell-when-selected你可以使用`beginUpdates来做动画`/`endUpdates`而不是`reloadData` (3认同)

ban*_*isa 12

要从@David略微改进上面的答案并回答有关如何在旋转过程中平滑地设置动画的最后评论,请使用以下方法:

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)

当然还有高度委托方法:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        return 171.0f;
    } else {
        return 128.0f;
    }
}
Run Code Online (Sandbox Code Playgroud)