我最近在新項目上工作,我需要在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