ios UITableViewCell 的默认高度是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/594852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:41:54  来源:igfitidea点击:

What is the default height of UITableViewCell?

iosiphoneuitableview

提问by Rob

I thought this information would have been easier to find :-)

我认为这些信息会更容易找到:-)

What is the default height of a UITableViewCell? It looks like 44 pixels, but I'd prefer to be sure.

UITableViewCell 的默认高度是多少?它看起来像 44 像素,但我更愿意确定。

回答by Marco

It's 44 pixels. Definitely. I'll never forget that number.

它是 44 像素。确实。我永远不会忘记那个号码。

44px is also the default height for UIToolbar and UINavigationBar. (Both switch to 32px when autorotated to landscape orientation.)

44px 也是 UIToolbar 和 UINavigationBar 的默认高度。(当自动旋转到横向时,两者都切换到 32px。)

回答by simon_smiley

If you want the default dimension on any device you can use: UITableViewAutomaticDimension

如果你想在任何设备上使用默认尺寸,你可以使用: UITableViewAutomaticDimension

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

Although 44 pixels is currently the default this is a useful method if your app relies on having the default value set.

尽管 44 像素当前是默认值,但如果您的应用程序依赖于设置默认值,这是一种有用的方法。

回答by ma11hew28

When style = UITableViewStyleGrouped, the default height of the top & bottom cells is actually 45.0f(not 44.0f). And, if the grouped table is only one row the cell height will be 46.0f.

当 时style = UITableViewStyleGrouped,顶部和底部单元格的默认高度实际上是45.0f(不是44.0f)。并且,如果分组表只有一行,则单元格高度将为46.0f.

回答by brainjam

If you want to calculate this on the fly, just allocate a dummy table cell and read off its height

如果您想即时计算,只需分配一个虚拟表格单元格并读取其高度

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
int height = cell.frame.size.height ;

This way you defend against changes in future versions of iOS, although I suppose that is unlikely.

通过这种方式,您可以抵御未来 iOS 版本的变化,尽管我认为这不太可能。

回答by frank

"When style = UITableViewStyleGrouped, the default height of the top & bottom cells is actually 45.0f (not 44.0f). And, if the grouped table is only one row the cell height will be 46.0f." It's wrong!! 44.0f in fact! I just test it!

“当 style = UITableViewStyleGrouped 时,顶部和底部单元格的默认高度实际上是 45.0f(不是 44.0f)。而且,如果分组表只有一行,单元格高度将为 46.0f。” 这是错的!!实际上是44.0f!我只是测试一下!

回答by Ziltoid

That sounds about right. But to be sure you could load up Interface builder, put in a UITableViewCell into the project then check the size properties in the Inspector window. I do not have my MacBook with me right now so I cannot check. But if you don't get a better answer from someone, that is how you can check for yourself.

这听起来很对。但要确保您可以加载界面构建器,将 UITableViewCell 放入项目中,然后检查检查器窗口中的大小属性。我现在没有随身携带 MacBook,所以无法检查。但是,如果您没有从某人那里得到更好的答案,您可以通过这种方式自行检查。

回答by Dan Bray

In Swift 4and Swift 5simply use:

Swift 4Swift 5 中只需使用:

UITableView.automaticDimension

Using 44px won't suffice because it will vary with different screen pixel densities.

使用 44px 是不够的,因为它会随着不同的屏幕像素密度而变化。

回答by fl034

On iOS 12 the default height on iPhone X like devices (X, XS, XS Max, XR) is 49pt.

在 iOS 12 上,iPhone X 等设备(X、XS、XS Max、XR)上的默认高度为49pt

Other devices on iOS 12 still have 44ptas default. It's a subtle difference, but it feels like a good improvement in direct comparison.

iOS 12 上的其他设备仍然默认为44pt。这是一个微妙的区别,但在直接比较中感觉是一个很好的改进。

If you won't believe me, measure for yourself in this screenshot (don't forget to divide by 3)... ;)

如果您不相信我,请在此屏幕截图中自己衡量(不要忘记除以 3)... ;)