如何触发当UITextField在UITableViewCell resignFromFirstResponder.&l



我在UITableViewCell中有两个UITextFields,我想检查secondTextFieldresignFromFirstResponderfirstTextFieldsecondTextField空时,它会显示一个警告标签。请看下面我的代码。

import Foundation
import UIKit
import SnapKit
class TwoTextInputTableViewCell: UITableViewCell {
let firstTextField = UITextField()
let secondTextField = UITextField()
let stackView = UIStackView().then {
$0.axis = .horizontal
$0.distribution = .fillEqually
$0.spacing = 15
}
let warningLabel = UILabel().then {
$0.isHidden = true
$0.addCharacterSpacing(-0.1)
$0.font = .systemFont(ofSize: 12)
$0.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 {
$0.left.equalToSuperview().offset(24)
$0.right.equalToSuperview().offset(-24)
$0.top.equalToSuperview().offset(12)
$0.height.equalTo(56)
}
warningLabel.snp.makeConstraints {
$0.top.equalTo(stackView.snp.bottom).offset(12)
$0.height.equalTo(16)
$0.left.right.equalTo(stackView)
}

}
}

一旦它辞职,我想使warningLabel.isHidden = false和重载tableView的高度,但如何做到这一点?任何答案都是感激的!由于

您可以通过使用textfield delegate轻松实现

首先确认委托

let firstTextField = UITextField()
let secondTextField = UITextField()
firstTextField.delegate = self
secondTextField.delegate = self

extension TwoTextInputTableViewCell : UITextFieldDelegate{
func textFieldDidEndEditing(textField: UITextField) {
if textField == firstTextField {
//first textfield  resignFromFirstResponder
}else if textField == secondTextField{
//second textfield  resignFromFirstResponder
}
}
}

相关内容

  • 没有找到相关文章

最新更新