Swift:UITableViewCell中UITextField的委托不起作用
我正在尝试将我的Swift:UITableViewCell中UITextField的委托不起作用,swift,uitableview,delegates,uitextfielddelegate,Swift,Uitableview,Delegates,Uitextfielddelegate,我正在尝试将我的标题f的委托设置为我的UITableViewCell或我的UITableView。这就是我在UITableViewCell类中设置文本字段的方法 class AddAppointmentTableViewCell: UITableViewCell, UITextFieldDelegate, UITextViewDelegate { var view = UIView() var titleTF = UITextField() func addTitle() -> UIVi
标题f
的委托设置为我的UITableViewCell
或我的UITableView
。这就是我在UITableViewCell
类中设置文本字段的方法
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
类中:
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
}
而行
等于[cell.addTitle(),cell.addNote()]
我不明白为什么我的
UITableViewCell
中的textfieldDiBeginediting
方法没有被调用。您需要添加这一行
{
textField.delegate=self
}您需要添加这一行 { textField.delegate=self
}您必须调用awakeFromNib中的代理(有相同的问题):
您必须调用awakeFromNib中的代理(有相同的问题):
显示您的
AddAppointmentTableViewCell
您希望看到什么?您的tableview单元格和textfieldDiBeginediting
方法这就是您需要的所有内容吗?“titleTF.delegate=self”甚至没有人知道titleTF的定义位置和方式。显示您的AddAppointmentTableViewCell
您希望看到什么?您的tableview单元格和textfieldDiBeginediting
方法这就是您需要的一切吗?“titleTF.delegate=self”甚至没有人知道titleTF的定义位置和方式。
override func awakeFromNib() {
super.awakeFromNib()
textField.delegate = self
}