[英]How to dynamically calculate height of UITableViewCell according to device
I have a UITableViewController
now, and I have x cells for UITableView
, which is the same height with screen. 我现在有一个
UITableViewController
,并且有x个用于UITableView
单元格,它与屏幕的高度相同。 I don't want to scroll the UITableView
, and I want to divide whole screen into x parts with the same height. 我不想滚动
UITableView
,我想将整个屏幕分成相同高度的x个部分。
Do I have to check device type first, then calculate height of every cell? 我是否必须先检查设备类型,然后计算每个单元的高度? Then there are lots of
if
or switch
statement. 然后有很多
if
或switch
语句。 What the best way to achieve this? 什么是实现这一目标的最佳方法?
Thanks. 谢谢。
Assume you have a Navigation Bar, try this: 假设您有一个导航栏,请尝试以下操作:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let screenHeight = UIScreen.mainScreen().bounds.height
return (screenHeight - 64) / <number of cells>
}
If you don't have Nagivation Bar, just replace 如果您没有导航栏,只需更换
return (screenHeight - 64) / <number of cells>
with 同
return screenHeight / <number of cells>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.