Rah*_*yat 8 uitableview ios swift
如何以编程方式设置视图高度我已经尝试此代码
cell.viewMain.frame = CGRectMake(cell.viewMain.frame.origin.x, cell.viewMain.frame.origin.y, cell.viewMain.frame.size.width, 65.0)
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
更新:
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
}
Run Code Online (Sandbox Code Playgroud)
首先,rowHeight更改表中所有行的高度.如果您想要特定行的特定高度,请实现tableView:heightForRowAtIndexPath:方法.首先删除代码中的tbUpdate.rowHeight.
小智 5
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
}
Run Code Online (Sandbox Code Playgroud)
您可以尝试使用自动布局创建高度约束,然后将约束连接到出口。然后设置该约束的常数,
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()
Run Code Online (Sandbox Code Playgroud)
将其放入您的 cellForRowAtIndexPath 中
编辑:我将问题误解为在 cellView 中请求另一个视图,如果是这样,委托方法 heightForRowAtIndexPath 确实是正确的,如上面答案中所述。
一个例子:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
if x == 0{
return 100.0 //Choose your custom row height
} else {
return 50
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22098 次 |
| 最近记录: |