简体   繁体   English

如何根据设备动态计算UITableViewCell的高度

[英]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. 然后有很多ifswitch语句。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM