Swift: UITableViewCell 中的 UITextField 委托不起作用
Swift: Delegate of UITextField in UITableViewCell does not work
我正在尝试将 titleTF
的委托设置为 UITableViewCell
或 UITableView
。这就是我在 UITableViewCell
class.
中设置 textField 的方式
class AddAppointmentTableViewCell: UITableViewCell, UITextFieldDelegate, UITextViewDelegate {
var view = UIView()
var titleTF = UITextField()
func addTitle() -> UIView {
view = UIView(frame: CGRect(x: 0, y: 0, width: frame.width, height:
NSAttributedString(string: "Enter Title", attributes: [.font: UIFont.boldSystemFont(ofSize: 24),
.foregroundColor: UIColor.lightGray]).size().height+30))
titleTF.translatesAutoresizingMaskIntoConstraints = false
titleTF.textAlignment = .left
titleTF.delegate = self
titleTF.attributedText = NSAttributedString(string: "Enter Title", attributes: [.font: UIFont.boldSystemFont(ofSize: 24),
.foregroundColor: UIColor.lightGray])
titleTF.inputAccessoryView = toolBar
view.addSubview(titleTF)
titleTF.topAnchor.constraint(equalTo: view.topAnchor, constant: 15).isActive = true
titleTF.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -15).isActive = true
titleTF.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 15).isActive = true
titleTF.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -15)
return view
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("textFieldDidEndEditing")
}
override func draw(_ rect: CGRect) {
addSubview(view)
}
}
这是我的 UITableView
class:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return rows[indexPath.row].frame.height
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AddAppointmentTableViewCell
cell.view = rows[indexPath.row]
return cell
}
而rows
等于[cell.addTitle(), cell.addNote()]
我不明白为什么 UITableViewCell
中的 textFieldDidBeginEditing
方法没有被调用。
您需要添加这一行
{
textField.delegate = 自己
}
你必须在 awakeFromNib 中调用委托(有同样的问题):
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}
我正在尝试将 titleTF
的委托设置为 UITableViewCell
或 UITableView
。这就是我在 UITableViewCell
class.
class AddAppointmentTableViewCell: UITableViewCell, UITextFieldDelegate, UITextViewDelegate {
var view = UIView()
var titleTF = UITextField()
func addTitle() -> UIView {
view = UIView(frame: CGRect(x: 0, y: 0, width: frame.width, height:
NSAttributedString(string: "Enter Title", attributes: [.font: UIFont.boldSystemFont(ofSize: 24),
.foregroundColor: UIColor.lightGray]).size().height+30))
titleTF.translatesAutoresizingMaskIntoConstraints = false
titleTF.textAlignment = .left
titleTF.delegate = self
titleTF.attributedText = NSAttributedString(string: "Enter Title", attributes: [.font: UIFont.boldSystemFont(ofSize: 24),
.foregroundColor: UIColor.lightGray])
titleTF.inputAccessoryView = toolBar
view.addSubview(titleTF)
titleTF.topAnchor.constraint(equalTo: view.topAnchor, constant: 15).isActive = true
titleTF.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -15).isActive = true
titleTF.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 15).isActive = true
titleTF.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -15)
return view
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("textFieldDidEndEditing")
}
override func draw(_ rect: CGRect) {
addSubview(view)
}
}
这是我的 UITableView
class:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rows.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return rows[indexPath.row].frame.height
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AddAppointmentTableViewCell
cell.view = rows[indexPath.row]
return cell
}
而rows
等于[cell.addTitle(), cell.addNote()]
我不明白为什么 UITableViewCell
中的 textFieldDidBeginEditing
方法没有被调用。
您需要添加这一行
{
textField.delegate = 自己
}
你必须在 awakeFromNib 中调用委托(有同样的问题):
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}