【问题标题】:Custom UITableViewCell reverse indent when cell is editing编辑单元格时自定义 UITableViewCell 反向缩进
【发布时间】: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


    【解决方案1】:

    问题已解决! 我已将我的代码修改为:

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 2011-01-30
      • 2013-01-01
      • 2016-06-13
      • 2011-08-12
      • 1970-01-01
      相关资源
      最近更新 更多