ios 如何在 Swift 中以编程方式设置 UITableView 单元格高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39283167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i set UITableView cell height programmatically in Swift
提问by Rahmat Hidayat
How to set view height programmatically i have try this code
如何以编程方式设置视图高度我已经尝试过这段代码
cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 65.0)
But that is not working.
但这行不通。
UPDATE :
更新 :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("updateCell", forIndexPath: indexPath) as! UpdateCell
if self.data[indexPath.row] == "b" {
tbUpdate.rowHeight = 85
cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 65.0)
}else{
tbUpdate.rowHeight = 220
cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 200.0)
}
return cell
}
回答by Jan Franz Palngipang
first, rowHeight changes the height of all rows in your table. if you want specific height for specific rows, implement tableView:heightForRowAtIndexPath:
method. remove tbUpdate.rowHeight in your code first.
首先,rowHeight 更改表中所有行的高度。如果您想要特定行的特定高度,请实现tableView:heightForRowAtIndexPath:
方法。首先删除代码中的 tbUpdate.rowHeight。
回答by i_am_jorf
TableView cells have their height set by the table view.
TableView 单元格的高度由表格视图设置。
You need to implement UITableViewDelegate tableView:heightForRowAtIndexPath:
.
您需要实施UITableViewDelegate tableView:heightForRowAtIndexPath:
.
回答by Zayne ZH
You can try using autolayout to create a height constrait and then connect the constraint to an outlet. Then set the constant for that constraint,
您可以尝试使用自动布局来创建高度约束,然后将约束连接到出口。然后为该约束设置常量,
var y: Float
if x==0{ //some condition
y = 10
}else if x==1{ //some condition
y = 20
}
cell.viewMainHeightConstraint.constant = y
cell.view.layoutIfNeeded()
Put this in your cellForRowAtIndexPath
把它放在你的 cellForRowAtIndexPath 中
Edit : I misinterpreted the question as asking for another view within the cellView, if so the delegate method heightForRowAtIndexPath is indeed correct as stated in the above answer.
编辑:我将问题误解为在 cellView 中要求另一个视图,如果是这样,则委托方法 heightForRowAtIndexPath 确实如上述答案中所述。
An example:
一个例子:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if x == 0{
return 100.0 //Choose your custom row height
} else {
return 50
}
}
回答by Beetroot
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var cellHeight:CGFloat = CGFloat()
if indexPath.row % 2 == 0 {
cellHeight = 20
}
else if indexPath.row % 2 != 0 {
cellHeight = 50
}
return cellHeight
}
回答by Vishal Vaghasiya
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var height:CGFloat = CGFloat()
if indexPath.row == 0 {
height = 80
}
else if indexPath.row == 1 {
height = self.view.frame.size.height - 44 - 64 // 44 is a tab bar height and 64 is navigationbar height.
print(height)
}
return height
}