简体   繁体   中英

How to clear textfield when row in UITableView is selected?

I have a table that enters in a string (from the table's cell label) wherever the user has a cursor - so functions like a keyboard with strings as the keys instead of individual characters.

I want to be able to delete that same string from whatever textfield the user is in (eg, Safari address bar) if the user clicks on the same row twice OR clicks a different row after selecting the first.

The code below inserts the text upon first clicking the row - how do I delete that exact text when clicking the row twice, or selecting a different row?

Note that the string to be inserted are all different sizes.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var string = buttonTitles[indexPath.row]
        (textDocumentProxy as! UIKeyInput).insertText("\(string)")
}

Use a variable to keep track of which row was pressed last. When didSelectRowAtIndexPath is called, compare the new row (from parameter) to the variable, and if they are the same, delete the word, and if they are different, delete the word and add the new word.

Maybe something like the code below. Not sure if the backspace technique will work, but it's something to try.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let newText = "\(buttonTitles[indexPath.row])"
    let oldText = "\(buttonTitles[lastRow])"
    var stringToAdd = ""

    //only delete the text if a row has already been selected
    if lastRow != -1      //-1 is a default value meaning there is no last row
    {
        //delete the old word
        for i in 0 ..< oldText.length
        {
            stringToAdd += "\b"      //add backspace characters to remove the old word
        }
    }

    //add the new word
    if indexPath.row != lastRow
    {
        stringToAdd += rowText
    }

    (textDocumentProxy as! UIKeyInput).insertText(stringToAdd)

    lastRow = indexPath.row
}

This code worked for me - similar to the comment above but uses the deleteBackward method, plus it unhighlights the row when selected twice. Need to define lastRow = -1 at the start of the class.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    var string = buttonTitles[indexPath.row]
    var stringLength = count(string)

    if lastRow == indexPath.row {
        for i in 1...stringLength {
            (textDocumentProxy as! UIKeyInput).deleteBackward()
        }
        self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
        lastRow = -1
    } else {
        (textDocumentProxy as! UIKeyInput).insertText("\(string)")
        lastRow = indexPath.row
    }
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    var string = buttonTitles[indexPath.row]
    var stringLength = count(string)
    for i in 1...stringLength {
        (textDocumentProxy as! UIKeyInput).deleteBackward()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM