Vetuka
Vetuka

Reputation: 1651

How to implement inputAccessoryView to UITextField in CustomCell using Swift?

I have a tableView with custom cell and it works perfectly with its own UITableViewCell configured.

In the custom cell there is an UITextField which uses keyboard type of Decimal Pad. So I wanted to add Done button as inputAccessoryView to the keyboard.

I am getting error on when assigning delegate to UITextField, using doneClicked function and textViewDidEndEditing function.

If I implement below code in my ViewController I am not getting any error but only that I cannot use UITextField outlet for in repetitive content (TableView).

Any ideas how to get this work? I appreciate any help!

class favCell: UITableViewCell, UITextFieldDelegate  {

    @IBOutlet weak var deciPadField: UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()

        self.deciPadField.delegate = self

        // Decipad config for adding Done button above
        let toolBar = UIToolbar()
        toolBar.sizeToFit()
        let flexiableSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked))
        toolBar.setItems([flexiableSpace, doneButton], animated: false)
        deciPadField.inputAccessoryView = toolBar  
    }


    @objc func doneClicked(){
        self.view.endEditing(true)
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        self.view.endEditing(true)
    }
}

Fixed

Upvotes: 0

Views: 1799

Answers (1)

Francesco Deliro
Francesco Deliro

Reputation: 3939

You are using UITextViewDelegate instead of UITextFieldDelegate. Then I suggest to set a protocol for your cell and to manage the text field delegate actions in your ViewController.

Upvotes: 2

Related Questions