在方向更改时更改UITableViewCell高度

时间:2010-04-27 01:36:34

标签: uitableview resize orientation

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

2 个答案:

答案 0 :(得分:13)

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

要确保在方向更改时再次调用tableView:heightForRowAtIndexPath:,您可能需要这样做 按照this question中的说明检测方向更改,然后调用[tableView reloadData]

答案 1 :(得分:12)

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

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

当然还有身份委托方法:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        return 171.0f;
    } else {
        return 128.0f;
    }
}
相关问题