| У меня есть TableView (alertTableView) в моем контроллере view с пользовательским UITableViewCell (ячейкой). В этот UITableViewCell я хочу установить некоторые ограничения;Как использовать ограничение VFL для пользовательской ячейки в tableview
- клетке требуется, чтобы изменить размер по горизонтали, чтобы соответствовать ширине экрана и имеет интервал 50pts . Лево и право.
- высота ячейки 10pts
- между ячейками я хочу иметь небольшое пространство из 2 точек по вертикали.
Для этого я использовал VFL для добавления ограничений, но ограничения не установлены/не соблюдаются. Я предполагаю, что мне нужно установить ограничения на AlertTableView, а не на просмотр. Пробовал это в любом случае, но, как и ожидалось от меня, это не имело никакого значения.
/*
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))
Что случилось?
== новой редакции примере ==
// 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)
при добавлении ошибочно ячейку или UILabel к UITableView, как alertTableView.addSubview(cell.reportCellDateTimeOutlet)
скрытая ячейка добавляется к UITableView, который становится видимым при перемещении вниз верхнюю ячейку. Это было неправильно, как указано в bsmith11.
Тем не менее при использовании пользовательских ячейки в 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)
Как предложено bsmith11 я переместил код из ViewController к классу продлевая UITableViewCell. Также было изменено программное применение ограничений от VFL до привязок Layout. Не работает для меня, к сожалению.
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
...
Я действительно имел в виду, что исправил его в вопросе. Однако проблема не устраняет. – pcvnes