检测文本字段应从 TableViewController 中的自定义 TableViewCell 返回以添加新行

Detecting textfieldshouldreturn from Custom TableViewCell in TableViewController to Add New Row

我想在用户按下自定义 TableViewCell 内的 return 键时在我的 TableView 中添加一个新行,其中包括一个 TextField。但是,我找不到这样做的方法...如何在我的 TableView 中查看 TextField 的事件以便我可以添加该行?

我的 TableViewController

class TableViewController: UITableViewController, CustomCellDelegate,
UITextFieldDelegate {

    var rowCount = 1

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

    // MARK: - Table view data source

    ...

    // Doesn't Do Anything
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        let indexPath = IndexPath(row: rowCount-1, section: 1)
        tableView.insertRows(at: [indexPath], with: .automatic)
        view.endEditing(true)
        return true
    }

    // Also does nothing
    func didReturn(cell: AddActivityTableViewCell, string: String?) {
        let indexPath = IndexPath(row: rowCount-1, section: 1)
        tableView.insertRows(at: [indexPath], with: .automatic)
        view.endEditing(true)
        rowCount += 1
    }

我的 CustomTableViewCell

protocol CustomCellDelegate: class {
    func didReturn(cell: CustomTableViewCell, string: String?)
}

class CustomTableViewCell: UITableViewCell, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!
    weak var delegate: CustomCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        textField.delegate = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    public func configureTextField(text: String?, placeholder: String) {
        textField.text = text
        textField.placeholder = placeholder

        textField.accessibilityValue = text
        textField.accessibilityLabel = placeholder
    }

    public func editableTextField(editable: Bool) {
        if editable == true {
            textField.isEnabled = true
        } else {
            textField.isEnabled = false
        }
    }

    // This works
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        delegate?.didReturn(cell: self, string: textField.text)
        return true
    }
}

谢谢!

我认为您错过了单元格中的设置委托。请在下面找到适合我的代码

ViewController

class TableViewController: UITableViewController, CustomCellDelegate, UITextFieldDelegate {


var rowCount = 1

override func viewDidLoad() {
    super.viewDidLoad()

}

// MARK: - Table view data source

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

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


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell : CustomTableViewCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
    cell.textField.placeholder = "Row \(indexPath.row)"
    cell.delegate = self
    return cell
}

func didReturn(cell: CustomTableViewCell, string: String?) {
    rowCount += 1
    let indexPath = IndexPath(row: rowCount-1, section:0)
    tableView.beginUpdates()
    tableView.insertRows(at: [indexPath], with: .automatic)
    tableView.endUpdates()
    view.endEditing(true)
}


}

自定义单元格

protocol CustomCellDelegate: class {
    func didReturn(cell: CustomTableViewCell, string: String?)
}

class CustomTableViewCell: UITableViewCell, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!
    weak var delegate: CustomCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        textField.delegate = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    public func configureTextField(text: String?, placeholder: String) {
        textField.text = text
        textField.placeholder = placeholder

        textField.accessibilityValue = text
        textField.accessibilityLabel = placeholder
    }

    public func editableTextField(editable: Bool) {
        if editable == true {
            textField.isEnabled = true
        } else {
            textField.isEnabled = false
        }
    }

    // This works
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        delegate?.didReturn(cell: self, string: textField.text)
        return true
    }

}