UITableViewCell中的UITextField resignFromFirstResponder时如何触发
How to trigger when UITextField in UITableViewCell resignFromFirstResponder
我在 UITableViewCell 中有两个 UITextField,我想检查当 secondTextField
resignFromFirstResponder
和 firstTextField
和 secondTextField
都为空时,它会显示一个警告标签。请看下面我的代码。
import Foundation
import UIKit
import SnapKit
class TwoTextInputTableViewCell: UITableViewCell {
let firstTextField = UITextField()
let secondTextField = UITextField()
let stackView = UIStackView().then {
[=10=].axis = .horizontal
[=10=].distribution = .fillEqually
[=10=].spacing = 15
}
let warningLabel = UILabel().then {
[=10=].isHidden = true
[=10=].addCharacterSpacing(-0.1)
[=10=].font = .systemFont(ofSize: 12)
[=10=].textColor = UIColor.CommonColor.salmonColor
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubviews(stackView, warningLabel)
stackView.addArrangedSubview(firstTextField)
stackView.addArrangedSubview(secondTextField)
stackView.snp.makeConstraints {
[=10=].left.equalToSuperview().offset(24)
[=10=].right.equalToSuperview().offset(-24)
[=10=].top.equalToSuperview().offset(12)
[=10=].height.equalTo(56)
}
warningLabel.snp.makeConstraints {
[=10=].top.equalTo(stackView.snp.bottom).offset(12)
[=10=].height.equalTo(16)
[=10=].left.right.equalTo(stackView)
}
}
}
一旦退出,我想warningLabel.isHidden = false
重新加载tableView的高度,但是怎么办呢?任何答案表示赞赏!谢谢
您可以使用文本字段委托轻松实现它
先向代表们确认
let firstTextField = UITextField()
let secondTextField = UITextField()
firstTextField.delegate = self
secondTextField.delegate = self
观察 textFieldDidEndediting 委托
extension TwoTextInputTableViewCell : UITextFieldDelegate{
func textFieldDidEndEditing(textField: UITextField) {
if textField == firstTextField {
//first textfield resignFromFirstResponder
}else if textField == secondTextField{
//second textfield resignFromFirstResponder
}
}
}
我在 UITableViewCell 中有两个 UITextField,我想检查当 secondTextField
resignFromFirstResponder
和 firstTextField
和 secondTextField
都为空时,它会显示一个警告标签。请看下面我的代码。
import Foundation
import UIKit
import SnapKit
class TwoTextInputTableViewCell: UITableViewCell {
let firstTextField = UITextField()
let secondTextField = UITextField()
let stackView = UIStackView().then {
[=10=].axis = .horizontal
[=10=].distribution = .fillEqually
[=10=].spacing = 15
}
let warningLabel = UILabel().then {
[=10=].isHidden = true
[=10=].addCharacterSpacing(-0.1)
[=10=].font = .systemFont(ofSize: 12)
[=10=].textColor = UIColor.CommonColor.salmonColor
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubviews(stackView, warningLabel)
stackView.addArrangedSubview(firstTextField)
stackView.addArrangedSubview(secondTextField)
stackView.snp.makeConstraints {
[=10=].left.equalToSuperview().offset(24)
[=10=].right.equalToSuperview().offset(-24)
[=10=].top.equalToSuperview().offset(12)
[=10=].height.equalTo(56)
}
warningLabel.snp.makeConstraints {
[=10=].top.equalTo(stackView.snp.bottom).offset(12)
[=10=].height.equalTo(16)
[=10=].left.right.equalTo(stackView)
}
}
}
一旦退出,我想warningLabel.isHidden = false
重新加载tableView的高度,但是怎么办呢?任何答案表示赞赏!谢谢
您可以使用文本字段委托轻松实现它
先向代表们确认
let firstTextField = UITextField()
let secondTextField = UITextField()
firstTextField.delegate = self
secondTextField.delegate = self
观察 textFieldDidEndediting 委托
extension TwoTextInputTableViewCell : UITextFieldDelegate{
func textFieldDidEndEditing(textField: UITextField) {
if textField == firstTextField {
//first textfield resignFromFirstResponder
}else if textField == secondTextField{
//second textfield resignFromFirstResponder
}
}
}