编辑单元格时自定义UITableViewCell反向缩进

编辑单元格时自定义UITableViewCell反向缩进,uitableview,indentation,reverse,Uitableview,Indentation,Reverse,我在Swift中自定义了一个UITableViewCell类。我在此类中定义了一个textField,并向其添加了约束 class DBCell: UITableViewCell { var textField:UITextField? required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) textLabel.setTransl

我在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))

}
}
let vBinds=NSDictionary(objectsAndKeys:textLabel,"textLabel")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[textLabel]-|", options: nil, metrics: nil, views: vBinds))                
当我从tableView(tableView:UITableView,cellForRowAtIndexPath-indexPath:NSIndexPath)创建自定义单元格并在UITableView中设置编辑时,它看起来很好。 但当我选择一个自定义单元格时,该单元格出现凹陷,看起来很难看。 为什么自定义单元格向左和向上移动?我如何解决这个问题?任何建议都应该感谢

let vBinds=NSDictionary(objectsAndKeys:textLabel,"textLabel")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[textLabel]-|", options: nil, metrics: nil, views: vBinds))                
今天,我为DBCell添加了约束:

let vBinds=NSDictionary(objectsAndKeys:textLabel,"textLabel")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[textLabel]-|", options: nil, metrics: nil, views: vBinds))                
我发现编辑时单元格包含中心,但单元格仍向左移动。我尝试向textLabel添加水平约束,如下所示:

let vBinds=NSDictionary(objectsAndKeys:textLabel,"textLabel")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[textLabel]-|", options: nil, metrics: nil, views: vBinds))                
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[textLabel]-|", options: nil, metrics: nil, views: hBinds))
但它不起作用。

问题解决了!
let vBinds=NSDictionary(objectsAndKeys:textLabel,"textLabel")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[textLabel]-|", options: nil, metrics: nil, views: vBinds))                
我已将代码修改为:

let vBinds=NSDictionary(objectsAndKeys:textLabel,"textLabel")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[textLabel]-|", options: nil, metrics: nil, views: vBinds))                
class DBCell: UITableViewCell {
   var textField:UITextField?
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
    required override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
}
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!)
textField?.setTranslatesAutoresizingMaskIntoConstraints(false)
let hBinds=NSDictionary(objectsAndKeys:textField!,"textField")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[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))
现在,我没有将任何约束设置为textLabel,只将约束设置为textField,我得到了正确的对齐方式textField。

let vBinds=NSDictionary(objectsAndKeys:textLabel,"textLabel")
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[textLabel]-|", options: nil, metrics: nil, views: vBinds))