wooden_lightsaber
wooden_lightsaber

Reputation: 17

How to implement actions after tapping on backspace key?

I’m trying to build a simple todo app. Task screen is a table view controller and it’s cells contain the “check” button and textfield for user input. I’m trying to recreate some logic iPhone’s notes app has. After user taps return key on the keyboard a new cell creates. If textfield is empty and user taps backspace key current cell deletes. The first thing works just fine, but I can’t put my mind on the second one. I found some way to register tapping on backspace, but have problems with understanding how to call for deleting cell method I made in a table view controller.

class CastomTextField: UITextField {

override public func deleteBackward() {
    if text == "" {
        print("Backspace tapped to delete row")
        // would like to insert delete row method here
    } else {
        print("Backspace tapped")
    }

    super.deleteBackward()
}

Maybe there are other ways to register tapping on backspace and implement actions?

Upvotes: 0

Views: 153

Answers (4)

Dilan
Dilan

Reputation: 2698

class CastomTextField: UITextField {

override public func deleteBackward() {
    if text == "" {
        print("Backspace tapped to delete row")
        myTable.deleteRows(at: [IndexPath(row: myTable.numberOfRows(inSection: 0)-1, section: 0)], with: .automatic)
        myTable.reloadData()
    } else {
        print("Backspace tapped")
    }

    super.deleteBackward()
}

Upvotes: 0

Abdul Karim Khan
Abdul Karim Khan

Reputation: 4945

You can subclass UITextField and add it to your desired class.Then override the method "deleteBackward()" which will catch all the backspace events.

override func deleteBackward() {
    super.deleteBackward()
    // Enter whatever you want to perform here
}

Upvotes: 0

Sumesh Sivan
Sumesh Sivan

Reputation: 213

In CustomTextField

protocol CustomTextFieldDelegate {
    func deleteRow(at index: Int)
}

class CustomTextField: UITextField {
    weak var delegate: CustomTextFieldDelegate?
    override public func deleteBackward() {
        if text == "" {
            print("Backspace tapped to delete row")
            // would like to insert delete row method here
            delegate?.deleteRow(at: self.tag) //Set the row index
        } else {
            print("Backspace tapped")
        }
        super.deleteBackward()
    }
}

In Your Custom Cell

class YourCustomCell: UITableViewCell {
    @IBOutlet weak var textField: CustomTextField!
}

In the table view controller where you create you cell do this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(
        withIdentifier: tableViewCellIdentifier, for: indexPath) as? YourCustomCell
    cell?.textField.tag = indexPath.row // Set the row index as tag so that we can retrieve later
    cell?.textField.delgate = self
    return cell!
}

Add delegate callback handler to you table view controller class

extension YourTableViewController: CustomTextFieldDelegate {
    func deleteRow(at index: Int) {
       // Call your method to delete cell at index.
    }
}

Upvotes: 1

Manoj
Manoj

Reputation: 1069

You can start with registering UIControlEvents.EditingChanged event

textField.addTarget(self, action: "textFieldChanged:", forControlEvents: UIControlEvents.EditingChanged)

Implement the textFieldChanged method, when you reach at the end of the cell, call delete cell method. You will need the cell indexPath for deleting cell. I recommend you find a way to get a selectedIndexPath.

    func textFieldChanged(textField: UITextField) {
        if textField.text?.isEmpty == true {
            textField.endEditing(true)
            deleteCurrentCell(indexPath: selectedIndexPath)
        }
    }

    private func deleteCurrentCell(indexPath: IndexPath) {
        tableView.beginUpdates()
        tableView.deleteRows(at: [indexPath], with: .right)
        tableView.endUpdates()
    }

Upvotes: 0

Related Questions