简体   繁体   English

在向包含UITextFields的UITableView单元格中输入文本时控制键盘

[英]Control of keyboard when entering text into UITableView cells containing UITextFields

I have a UITableView with custom cells containing two UITextFields for text entry. 我有一个UITableView,其中的自定义单元格包含两个用于文本输入的UITextFields。 I would like the keyboard to stay in place until text entry is finished but when a second textField is tapped, the keyboard disappears and I have to make a second tap to get it back. 我希望键盘一直保持在原位置,直到完成文本输入为止,但是当敲击第二个textField时,键盘消失了,我必须再按一下以将其取回。 I understand that the first tap has triggered a 'resignFirstResponder' message, but how can I keep the keyboard in place? 我了解第一次敲击会触发“ resignFirstResponder”消息,但是如何使键盘保持在原位? Have tried setting the tableScrollView keyboardDissmissMode, but it makes no difference. 尝试设置tableScrollView keyboardDissmissMode,但没有区别。 I implement 'textFieldDidEndEditing' to store the text entered and declare the view controller to be the texField's delegate.Am using Swift 4 with iOS 12. 我实现了'textFieldDidEndEditing'来存储输入的文本,并将视图控制器声明为texField的委托.Am在iOS 12上使用Swift 4。

This is the code for the custom cell: 这是自定义单元格的代码:

class ChoreoCell: UITableViewCell, UITextFieldDelegate { var inputText:String? 类ChoreoCell:UITableViewCell,UITextFieldDelegate {var inputText:String?

@IBOutlet weak var countText: UITextField! {
    didSet {
        countText.sizeToFit()
        countText.text = inputText
    }
}

@IBOutlet weak var stepText: UITextField! {
    didSet {
        stepText.sizeToFit()
        stepText.text = inputText
    }
} 

} }

... and this is the code I am using in a tableViewController: ...这是我在tableViewController中使用的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell( withIdentifier:Storyboard.CellIdentifier, for: indexPath) as? ChoreoCell
    if (self.choreography.count == 0 ) {
        let myCountSteps = CountSteps(counts:"", steps:"" )
        for _ in 0...Storyboard.InitialRows-1 {
            self.choreography.append(myCountSteps)
        }
    }
    cell!.countText.text = self.choreography[indexPath.row].counts
    cell!.stepText.text = self.choreography[indexPath.row].steps
    cell!.countText.delegate = self
    cell!.stepText.delegate = self
    return cell!
}

func textFieldDidEndEditing(_ textField: UITextField) {
    guard let txt : String = textField.text else {
        print ("input error for text, editing ended but no text received")
        return
    }
    var v:UIView = textField
    repeat { v = v.superview!} while !(v is UITableViewCell)
    if let cell = v as? ChoreoCell {
        guard let ip = self.tableView.indexPath(for: cell) else {
            print("Error identifying index path for \(cell)")
            return
        }            
        switch (textField) {
            case cell.countText:
                let myCountSteps = CountSteps(counts:txt, steps:cell.stepText.text! )
                self.choreography[ip.row] = myCountSteps
            case cell.stepText:
                let myCountSteps = CountSteps(counts:cell.countText.text!, steps:txt )
                self.choreography[ip.row] = myCountSteps
            default:
                print ("Unexpected textField case")
        }
        self.danceDoc?.choreography[ip.row] = self.choreography[ip.row]
        self.danceDoc?.updateChangeCount(.done)
        return
    } else {
        print ("Error identifying cell from text entry")
        return
    }
} 

How can I determine which textField is tapped in to end the editing of the current textField, so I can assign it first responder status and prevent the keyboard disappearing? 如何确定要输入哪个textField来结束当前textField的编辑,以便可以将其分配为第一响应者状态并防止键盘消失?

You probably have similar code to this, which will cause the keyboard dismissal no matter what: 您可能具有与此类似的代码,无论如何,这都会导致键盘被解雇:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()            
    return true
}

To fix it - you need to programmatically activate the next Text Field: 要解决此问题-您需要以编程方式激活下一个“文本字段”:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()       
    self.nextTextField.becomeFirstResponder() // Add this line
    return true
}

You may need to do additional check, because for the last field you may actually need to dismiss the keyboard. 您可能需要进行其他检查,因为对于最后一个字段,您实际上可能需要关闭键盘。

Not 100% sure if it won't affect other functionality, but I believe even textField.resignFirstResponder() line can be omitted (in case we want the keyboard to remain on the screen), otherwise it may cause some weird animation of the keyboard. 不能100%确定它是否不会影响其他功能,但我相信甚至可以省略textField.resignFirstResponder()行(以防我们希望键盘保留在屏幕上),否则可能会导致键盘产生一些怪异的动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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