Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView单元格-如何制作一个单元格';s高度固定,其他单元格';相对于屏幕大小的s高度变量?_Ios_Swift_Uitableview_Height_Tableviewcell - Fatal编程技术网

Ios UITableView单元格-如何制作一个单元格';s高度固定,其他单元格';相对于屏幕大小的s高度变量?

Ios UITableView单元格-如何制作一个单元格';s高度固定,其他单元格';相对于屏幕大小的s高度变量?,ios,swift,uitableview,height,tableviewcell,Ios,Swift,Uitableview,Height,Tableviewcell,我有一个静态UITableView,有1个部分,2个单元格 我想'底部'细胞是固定高度70像素和'顶部'细胞的可变高度-以填补屏幕的平衡。详情如下: 我有以下代码: //Modify TableView Cell Heights For Screen Sizes: var absoluteCellHeights: [CGFloat] = [300, 70] { didSet { tableView.reloadData() } } var normalised

我有一个静态UITableView,有1个部分,2个单元格

我想'底部'细胞是固定高度70像素和'顶部'细胞的可变高度-以填补屏幕的平衡。详情如下:

我有以下代码:

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

var normalisedCellHeights: [CGFloat]? {
    let totalHeight = absoluteCellHeights.reduce(0) { $0 + $1 }
    let normalisedHeights: [CGFloat]? = totalHeight <= 0 ? nil : absoluteCellHeights.map { $0 / 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
}
//修改屏幕大小的TableView单元格高度:
var absoluteCellHeights:[CGFloat]=[300,70]{
迪塞特{
tableView.reloadData()
}
}
var normalisedCellHeights:[CGFloat]?{
让totalHeight=absoluteCellHeights.reduce(0){$0+$1}
设normalisedHeights:[CGFloat]?=总高度CGFloat{
让高度:CGFloat
如果让normalisedHeight=self.normalisedCellHeights?[indexPath.row]{
高度=标准高度*tableView.frame.height
}否则{
高度=50.0//只是一个随机值。
}
返回高度
}
…这在我的最小屏幕尺寸目标(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
}
这应该起作用:

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
}

您的直觉是正确的,您希望使用
高度作为rowatinexpath

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

如果这不起作用,请用出错的地方进行注释,我将修改我的答案。

你的直觉是正确的,你想使用
高度来表示rowatinexpath

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

如果这不起作用,请用出现问题的地方进行评论,我将修改我的答案。

为什么要使用表视图而不是只有两个视图和两个布局约束?这是一个非常好的问题。本质上,我有一系列不同的表视图控制器-一些有两个单元格,一些有更多单元格。但底部单元格始终具有恒定的高度ht.^也同意。如果您的表视图不是动态的,只需使用两个视图,一个高度约束为70,另一个固定在其余的边上。为什么要使用一个表视图而不是两个布局约束的视图?这是一个很好的问题。从本质上说,我有一系列不同的TableViewController,其中一些具有2 cells,有些有更多。但是底部单元格的高度总是恒定的。^也同意。如果您的表视图不是动态的,只需使用两个视图,一个高度约束为70,另一个固定在其余的边上。啊。简单多了;)默认情况下,它将返回行中的任何其他单元格!=1来填充屏幕的平衡?I'我会尝试并报告。如果您有两个以上的单元格,您可以简单地将检查更改为:
If indexath.row==self.data.count-1
其中
data
是您提供给表视图的日期数组。逻辑非常棒。值得一个功能区和蝴蝶结;)非常感谢。非常适合我的需要!不过我还是建议您这样做如果您的表视图内容不是动态的,则使用带有简单视图的自动布局,除非您需要表视图附带的外观。您能详细说明一下吗?对于当前包含两(2)个布局约束策略的所有TableViewController,我将采用两(2)个UIView(谢谢@Wain)tableview单元格-对我来说更简单。但是还有其他原因吗?啊,这么简单;)默认情况下会返回任何其他行!=1的单元格来填充屏幕的剩余部分吗?我将尝试并报告。如果有两个以上的单元格,您只需将检查更改为:
If indexath.row==self.data.count-1
其中
数据
是您提供给表视图的日期数组。非常棒的逻辑。值得一个功能区和蝴蝶结;)非常感谢。非常适合我的需要!如果表视图内容不是动态的,我建议使用带有简单视图的自动布局,除非您需要表视图的外观。可以吗请详细说明一下?我将采用两(2)个UIView和两个布局约束策略(感谢@Wain),用于所有当前包含两(2)个tableview单元格的TableViewController,这对我来说更简单。但还有其他原因吗?