我最近在新项目上工作,我需要在TableViewController中的一个单元格中添加两个自定义视图。像这样:iOS Swift在TableView Cell中添加两个视图for for循环
if indexPath.row == 4 {
for index in 0..<2 {
let reviewView = ReviewView()
reviewView.translatesAutoresizingMaskIntoConstraints = false
cell.addSubview(reviewView)
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[v0]-|",options: [],metrics: nil,views: ["v0" : reviewView]))
if index == 0 {
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[v0(71)]",options: [],metrics: nil,views:["v0" : reviewView]))
reviewView.backgroundColor = UIColor.redColor()
reviewView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, 71)
}
if index == 1 {
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[v0(71)]-|",options: [],metrics: nil,views:["v0" : reviewView]))
reviewView.backgroundColor = UIColor.blueColor()
reviewView.frame = CGRectMake(0, 71, cell.contentView.frame.size.width, 71)
}
print(index)
}
return cell
}
但结果是这样的(两个视图的高度必须等于和71):
我想这个问题是关于约束两者之间view.How我管理呢?
谢谢你们
编辑: 室高度为汽车(UITableViewAutomaticDimension)
什么是您的ReviewView框架? – Sofeda
你的细胞高度是多少?是71 * 2? – Sofeda
@SMi它的UITableViewAutomaticDimension(自动高度)和ReviewView是我xib的自定义视图 – MohammadReza