gpt4 book ai didi

ios - 如何使单元格高度适合屏幕

转载 作者:行者123 更新时间:2023-11-28 19:51:15 26 4
gpt4 key购买 nike

我有一个 UITableView 并且有 10 行(这是一个动态表,因此将加载更多行)。

每个单元格/行都应具有全屏的高度。 iOS 现在有不同的屏幕尺寸。

因此我无法为单元格行提供固定高度,因为 iOS 设备具有不同的高度。

那么如何使单元格高度适合屏幕高度呢?

最佳答案

如果您的 tableView 在 ViewController 中,那么您可以使用下面的代码:

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

return CGRectGetHeight(self.view.bounds);
}

否则如果你的屏幕覆盖全屏,那么使用这个

return CGRectGetHeight([[UIScreen mainScreen] bounds]);

否则,如果您的屏幕也包含导航栏,则使用

return CGRectGetHeight([[UIScreen mainScreen] bounds] - 64.0f);

或者最好的解决方案可能是

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

return CGRectGetHeight(tableView.bounds);
}

已编辑

最好使用 rowHeight 属性而不是 delegate。原因如下:

There are performance implications to using tableView:heightForRowAtIndexPath: instead of rowHeight. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).

感谢rdelmar

Source

关于ios - 如何使单元格高度适合屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29114289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com