1

一旦设备旋转,我在 UITableView 中显示具有正确高度的 UITableViewCells 时遇到问题。最初加载表格视图时,所有行都具有正确的大小,如下图所示:

正确的行高

但是,当我将设备旋转到横向模式时,我得到的行比我想要的要短,因此,部分视图背景是可见的,请参见此处: 行太短,表格视图底部可见视图背景

再次将手机转回纵向模式时,行大小比第一次加载视图时更大,因此底部单元格看起来更小,并且文本看起来不在单元格中居中,请参见此处:

单元格,比第一次加载时略高

这是我用于行高计算的方法和变量:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    CGFloat statusBarWidth = [UIApplication sharedApplication].statusBarFrame.size.width;
    CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
    CGFloat rowHeight;

    NSUInteger numberOfRows = self.orderedCategories.count;

    if (orientation == UIDeviceOrientationPortrait) {
        rowHeight = ((screenHeight - navigationBarHeight - statusBarHeight) / numberOfRows);
        NSLog(@"portrait: %f", rowHeight);
    } else {
        rowHeight = ((screenWidth - navigationBarHeight - statusBarWidth) / numberOfRows);
        NSLog(@"landscape: %f", rowHeight);
    }

    return rowHeight;
}
4

1 回答 1

0

我没有明确的答案,但这里有几点需要考虑:

1) 你为什么在 tableView:heightForRowAtIndexPath 中调用 [self.tableview setNeedsDisplay]?如果您需要在此处重置显示,则可能是您的整体逻辑有问题。

2) 将计算代码移出到 didRotateFromInterfaceOrientation: 方法怎么样?完成后调用 reloadData。将高度存储在实例变量中,然后在 tableView:heightForRowAtIndexPath: 中返回它。

2)此代码是否在旋转后运行。尝试注销不同的常量并手动仔细检查数学。这可能会让您更好地了解正在发生的事情。可能存在时间问题 - 例如,您可能在旋转发生之前而不是在它发生之后获取状态栏高度。

于 2013-11-14T00:40:08.963 回答