UITableView Cells - 如何使一个单元格的高度固定,其他单元格的高度相对于屏幕尺寸可变?

UITableView Cells - How to make one cell's height fixed, other cell's height variable relative to screen size?

我有一个包含 1 个部分、2 个单元格的静态 UITableView。

我希望 'bottom' 单元格的高度固定为 70 像素,而 'top' 单元格的高度可变 - 以填充屏幕的平衡。如下:

我有这个代码:

//Modify TableView Cell Heights For Screen Sizes:
var absoluteCellHeights: [CGFloat] = [300, 70] {
    didSet {
        tableView.reloadData()
    }
}

var normalisedCellHeights: [CGFloat]? {
    let totalHeight = absoluteCellHeights.reduce(0) { [=10=] +  }
    let normalisedHeights: [CGFloat]? = totalHeight <= 0 ? nil : absoluteCellHeights.map { [=10=] / totalHeight }

    return normalisedHeights
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    let height: CGFloat


    if let normalisedHeight = self.normalisedCellHeights?[indexPath.row] {
        height = normalisedHeight * tableView.frame.height
    } else {
        height = 50.0 // Just a random value.
    }

    return height
}

...这在我的最小屏幕尺寸目标(3.5 英寸)上运行良好,我为两个单元分配的总尺寸为 370 像素。

但是对于更大的屏幕尺寸,我的总尺寸分配会增加。使用上面的代码,两个单元格将以 300:70 相对比例显示。

我希望顶部单元格的大小可以根据屏幕尺寸变化 - 屏幕越大,单元格高度就越大。但底部单元格的大小保持70像素不变。

有人可以帮忙吗?非常感谢。

这应该有效:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    var height: CGFloat

    if indexPath.row == 1 {
        height = 70.0
    } else {
        height = tableView.frame.height - 70.0
    }

    return height
}

你的直觉是正确的,你想用heightForRowAtIndexPath

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == 1 { return 70 }
    else { return tableView.frame.size.height - 70 }
}

如果这不起作用,请评论哪里出了问题,我会修改我的答案。