在自定义单元格中编辑UITextView时调用textViewDidBeginEditing

时间:2017-10-08 22:28:32

标签: ios swift user-interface uitextfield tableview

在我的ViewController中编辑textview时,我似乎无法调用textViewDidBeginEditing函数。

这是我的自定义单元格的代码:

class NoteDetailCell: UITableViewCell {
    let noteText: UITextView = {
        let label = UITextView()
        label.font = UIFont(name: "Nunito",size: 16)
        label.backgroundColor = UIColor(r: 244, g: 244, b: 255)
        label.text = "Have to check out this food place. The best burgers in town."
        label.textColor = UIColor(r: 88, g: 88, b: 88)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: .`default`, reuseIdentifier: reuseIdentifier)

        self.selectionStyle = UITableViewCellSelectionStyle.none

        addSubview(noteText)

        noteText.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
        noteText.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8).isActive = true
        noteText.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8).isActive = true
    }
}

这是我的tableview代码:

class TripNoteViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {
    var noteDetails: UITableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()

        noteDetails.frame = CGRect(x: 0, y: 65, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height-140)

        noteDetails.delegate = self
        noteDetails.dataSource = self
        noteDetails.separatorColor = UIColor.clear
        noteDetails.register(NoteDetailCell.self, forCellReuseIdentifier: "detailsCell")
        noteDetails.register(NoteOriginCell.self, forCellReuseIdentifier: "visitTrip")
        noteDetails.register(NoteActionsCell.self, forCellReuseIdentifier: "noteActions")

    }

    func textViewDidBeginEditing(noteText: UITextView) {
        print("noteText text")
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: detailsCell) as! NoteDetailCell
        cell.noteText.isScrollEnabled = false
        cell.noteText.delegate = self
        return cell
    }
}

我想在编辑自定义单元格中的文本字段时从我的ViewController调用textViewDidBeginEditing函数。关于我出错的任何建议?

1 个答案:

答案 0 :(得分:0)

您尚未实现UITextViewDelegate协议定义的方法。变化:

func textViewDidBeginEditing(noteText: UITextView) {
    print("noteText text")
}

为:

func textViewDidBeginEditing(_ noteText: UITextView) {
    print("noteText text")
}

由于您的签名错误,因此不会调用。