ios - 使表格 View 单元格在 Swift 中完全填满屏幕

标签 ios swift uitableview

我在 Storyboard 中构建的 UITableView 中有三个单元格,当我运行它时,我在模拟器中得到了三个预期的单元格,然后是下面的空白区域。我想要做的是让单元格根据 iOS 设备动态填满屏幕。我该怎么做?

enter image description here

最佳答案

为单元格设置一个最小高度,如果项目很少,则使用偶数高度,否则使用minHeight。

let MinHeight: CGFloat = 100.0
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let tHeight = tableView.bounds.height

    let temp = tHeight/CGFloat(items.count)

    return temp > MinHeight ? temp : MinHeight
}

Swift 5 更新:

let minRowHeight: CGFloat = 100.0

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    let tHeight = tableView.frame.height
    let temp = tHeight / CGFloat(items.count)
    return temp > minRowHeight ? temp : minRowHeight
}

关于ios - 使表格 View 单元格在 Swift 中完全填满屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35994977/

相关文章:

ios - 无法将 sqlite3_last_insert_rowid 存储到数组中

ios - 我需要使我的 UIImageView 适合我的 UIScrollView 而无需平移。 [iOS]

iphone - 尝试重新购买时不调用 SKPaymentTransactionStateRestored

swift - 突出显示两个 CollectionView 中的单元格

swift - 重新使用的 ViewController 中的搜索栏在 segue 后没有响应

iOS 8 TableView Controller 性能问题

ios - 如何在 Swift 上的 Firebase 中获取嵌套键内的值

ios - 使用Apple分阶段发布应用程序时,第一次下载的用户会看到哪个版本?

arrays - Swift - 带数组的字典 - 获取对数组的引用

ios - 如何找出 UILabel wordwraps 的时间