【发布时间】:2014-10-23 07:56:36
【问题描述】:
我在 Swift 中自定义了一个 UITableViewCell 类。我在这个类中定义了一个 textField 并为其添加了约束。
class DBCell: UITableViewCell {
var textField:UITextField?
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
textLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
fatalError("init(coder:) has not been implemented")
}
required override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
textLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
}
}
class DBEditCell: DBCell{
required init(coder aDecoder: NSCoder) {
uper.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
if textField == nil
{
textField=UITextField(frame: CGRectZero)
}
textField?.textAlignment=NSTextAlignment.Left
textField?.clearButtonMode=UITextFieldViewMode.WhileEditing
textField?.enabled=true
self.shouldIndentWhileEditing=false
textField?.delegate=self
contentView.addSubview(textField!)
textLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
//Add constraints to textField
let hBinds=NSDictionary(objectsAndKeys:textLabel,"textLabel",textField!,"textField")
textField?.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[textLabel]-[textField]-|", options: nil, metrics: nil, views: hBinds))
let vBinds=NSDictionary(objectsAndKeys:textField!,"textField")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[textField]-|", options: nil, metrics: nil, views: vBinds))
}
}
当我从 tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) 创建自定义单元格并在 UITableView 中设置编辑时,它看起来不错。 但是当我选择一个自定义单元格时,该单元格突出并且看起来很难看。 为什么自定义单元格向左和向上移动?我该如何解决这个问题?任何建议都值得赞赏。
今天我给 DBCell 添加了约束:
let vBinds=NSDictionary(objectsAndKeys:textLabel,"textLabel")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[textLabel]-|", options: nil, metrics: nil, views: vBinds))
我发现在编辑时单元格包含中心,但单元格仍然向左移动。我尝试向 textLabel 添加水平约束,如下所示:
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[textLabel]-|", options: nil, metrics: nil, views: hBinds))
它没有用。
【问题讨论】:
标签: uitableview indentation reverse