[英]How to use VFL constraint to custom cell in tableview
|I have a TableView (alertTableView) in my viewcontroller with a custom UITableViewCell (cell). |我的视图控制器中有一个带有自定义UITableViewCell(单元格)的TableView(alertTableView)。 To this UITableViewCell i want to set some constraints;
我想为这个UITableViewCell设置一些约束。
To do this i used VFL to add the constraints, but the constraints are not set/respected. 为此,我使用VFL添加了约束,但是没有设置/遵守约束。 I assume that i have to set the constraints on the AlertTableView and not on view.
我假设我必须在AlertTableView而非视图上设置约束。 Tried it anyway, but as expected by myself this did not make a difference.
无论如何都尝试过,但是正如我自己所期望的那样,这没有什么不同。
/*
VFL constraints Table View Cell
*/
let cell:ReportTableviewCell = self.alertTableView.dequeueReusableCellWithIdentifier("cell") as! ReportTableviewCell
cell.translatesAutoresizingMaskIntoConstraints = false
alertTableView.addSubview(cell)
let cellDictionary = ["ReportTableviewCell": cell]
alertTableView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(50)-[ReportTableviewCell]-(50)-|",options: [], metrics: nil, views: cellDictionary))
alertTableView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[ReportTableviewCell(cellHeight)]-(cellVSpacing)-|",options: [], metrics: ["cellHeight" : 40, "cellVSpacing" : 2], views: cellDictionary))
What's done wrong ? 做错了什么?
== new revision of the example == ==该示例的新修订版==
// Register ReportCell
let nib = UINib(nibName: "ReportCell", bundle: nil)
alertTableView.registerNib(nib, forCellReuseIdentifier: "cell")
/*
VFL constraints Table View Cell
*/
let cell:ReportTableviewCell = self.alertTableView.dequeueReusableCellWithIdentifier("cell") as! ReportTableviewCell
cell.translatesAutoresizingMaskIntoConstraints = false
alertTableView.addSubview(cell)
var cellAllConstraints = [NSLayoutConstraint]()
let cellDictionary = ["ReportTableviewCell": cell,
"reportCellDateTime": cell.reportCellDateTimeOutlet]
let cellHorizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-50-[ReportTableviewCell]-50-|",options: [], metrics: nil, views: cellDictionary)
let cellVerticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[ReportTableviewCell(cellHeight)]-(cellVSpacing)-|",options: [], metrics: ["cellHeight" : 10, "cellVSpacing" : 2], views: cellDictionary)
cellAllConstraints += cellHorizontalConstraints
cellAllConstraints += cellVerticalConstraints
NSLayoutConstraint.activateConstraints(cellAllConstraints)
// set constraint on UILabel reportCellDateTimeOutlet in cell ReportCell
cellAllConstraints.removeAll()
let dateTimeHorizontalConstrains = NSLayoutConstraint.constraintsWithVisualFormat("H:|-50-[reportCellDateTime]-|",options: [], metrics: nil, views: cellDictionary)
cellAllConstraints += dateTimeHorizontalConstrains
NSLayoutConstraint.activateConstraints(cellAllConstraints)
when erroneously adding a cell or UILabel to an UITableView like alertTableView.addSubview(cell.reportCellDateTimeOutlet)
a hidden cell is added to the UITableView which becomes visible when dragging down the top cell. 错误地将单元格或UILabel添加到UITableView(如
alertTableView.addSubview(cell.reportCellDateTimeOutlet)
隐藏的单元格添加到UITableView中,当向下拖动顶部单元格时,该单元格将可见。 This was wrong as pointed out by bsmith11. 正如bsmith11指出的那样,这是错误的。
Still when using a custom cell in a UITableView and setting the constraints for a specific UILabel in that cell, the constraints are not affected. 仍然在UITableView中使用自定义单元格并在该单元格中设置特定UILabel的约束时,约束不会受到影响。
let cell:ReportTableviewCell = self.alertTableView.dequeueReusableCellWithIdentifier("cell") as! ReportTableviewCell
var cellAllConstraints = [NSLayoutConstraint]()
let cellDictionary = [
"reportCellDateTime": cell.reportCellDateTimeOutlet,
"reportCellViewedIndicator" : cell.reportCellViewedIndicatorOutlet,
"reportCellDescription" : cell.reportCellDescriptionOutlet ]
cell.reportCellDateTimeOutlet.translatesAutoresizingMaskIntoConstraints = false
let dateTimeHorizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-50-[reportCellDateTime(300)]-|",options: [], metrics: nil, views: cellDictionary)
cellAllConstraints += dateTimeHorizontalConstraints
NSLayoutConstraint.activateConstraints(cellAllConstraints)
As suggested by bsmith11 i moved the code from the ViewController to the class extending the UITableViewCell. 正如bsmith11所建议的那样,我将代码从ViewController移到了扩展UITableViewCell的类中。 Also changed the programmatically application of constraints from VFL to Layout anchors.
还以编程方式将约束的应用程序从VFL更改为布局锚。 Does not work for me unfortunately.
不幸的是对我不起作用。
class ReportTableviewCell : UITableViewCell {
@IBOutlet weak var reportCellViewedIndicatorOutlet: UIImageView!
@IBOutlet weak var reportCell: UIView!
override func awakeFromNib() {
super.awakeFromNib()
// Internal border
self.layer.borderColor = UIColor(red: 245/255, green: 166/255, blue: 35/255, alpha: 1.0).CGColor
self.layer.cornerRadius = 8.0
self.layer.masksToBounds = true
self.layer.borderWidth = 4.0
reportCell.translatesAutoresizingMaskIntoConstraints = false
reportCellViewedIndicatorOutlet.bottomAnchor.constraintEqualToAnchor(reportCell.bottomAnchor, constant: 10)
reportCellViewedIndicatorOutlet.rightAnchor.constraintEqualToAnchor(reportCell.rightAnchor, constant:-10)
reportCellDescriptionOutlet.translatesAutoresizingMaskIntoConstraints = false
reportCellDescriptionOutlet.leftAnchor.constraintEqualToAnchor(reportCell.leftAnchor, constant: 10).active = true reportCellDescriptionOutlet.rightAnchor.constraintEqualToAnchor(reportCell.rightAnchor, constant: 10).active = true
...
Did you mean: 你的意思是:
"H:|-(50)- ...
instead of 代替
"H:-(50)-| ...
You seem to be using UITableView
incorrectly. 您似乎未正确使用
UITableView
。
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10 https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10
You should be settings constraints on subviews inside of your UITableViewCell
subclass. 您应该在
UITableViewCell
子类内部的子视图上设置约束。 The UITableView
will handle layout of the actual cells. UITableView
将处理实际单元格的布局。
Finally i have been able to set constraints. 最后,我已经能够设定约束。 As suggested by @bsmith11 i switched to NSConstraints in favour of VFL.
正如@ bsmith11所建议的那样,我改用NSConstraints以支持VFL。 Also moved the programmatically defined constraints from the ViewController to the class extending the UITableViewCell class.
还将编程定义的约束从ViewController移到了扩展UITableViewCell类的类。
@IBOutlet weak var reportCellViewedIndicatorOutlet: UIImageView!
@IBOutlet weak var reportCell: UIView!
override func awakeFromNib() {
super.awakeFromNib()
reportCell.translatesAutoresizingMaskIntoConstraints = false
// viewed Indicator constraints
let viewedIndicatorConstraints: [NSLayoutConstraint] = [
reportCellViewedIndicatorOutlet.rightAnchor.constraintEqualToAnchor(self.rightAnchor, constant: -3),
reportCellViewedIndicatorOutlet.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor, constant: -3)
]
NSLayoutConstraint.activateConstraints(viewedIndicatorConstraints)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.