簡體   English   中英

如何根據設備動態計算UITableViewCell的高度

[英]How to dynamically calculate height of UITableViewCell according to device

我現在有一個UITableViewController ,並且有x個用於UITableView單元格,它與屏幕的高度相同。 我不想滾動UITableView ,我想將整個屏幕分成相同高度的x個部分。

我是否必須先檢查設備類型,然后計算每個單元的高度? 然后有很多ifswitch語句。 什么是實現這一目標的最佳方法?

謝謝。

假設您有一個導航欄,請嘗試以下操作:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let screenHeight = UIScreen.mainScreen().bounds.height
    return (screenHeight - 64) / <number of cells>
}

如果您沒有導航欄,只需更換

return (screenHeight - 64) / <number of cells>

return screenHeight / <number of cells>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM