Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当textField应返回时,UITableView中的多个textField会更新错误的textField_Ios_Swift_Uitableview_Becomefirstresponder - Fatal编程技术网

Ios 当textField应返回时,UITableView中的多个textField会更新错误的textField

Ios 当textField应返回时,UITableView中的多个textField会更新错误的textField,ios,swift,uitableview,becomefirstresponder,Ios,Swift,Uitableview,Becomefirstresponder,我正在使用自定义的UITableViewCells通过UITableView生成表单,其中每个单元格都包含一个UITextField 我正在使用textField shouldReturn跳转到下一个textField,但我无法理解为什么我在一个textField中键入的内容会随机出现在另一个textField中(实际上,存在奇怪的模式) 这里是自定义UITableViewCell RPTableViewCell类:UITableViewCell{ @IBVAR弱var标题标签:UILabel!

我正在使用自定义的
UITableViewCell
s通过UITableView生成表单,其中每个单元格都包含一个
UITextField

我正在使用
textField shouldReturn
跳转到下一个textField,但我无法理解为什么我在一个textField中键入的内容会随机出现在另一个textField中(实际上,存在奇怪的模式)

这里是自定义UITableViewCell

RPTableViewCell类:UITableViewCell{
@IBVAR弱var标题标签:UILabel!
@IBMOutlet弱var文本字段:DictionaryTextField!
@IBVAR错误标签:UILabel!
重写func awakeFromNib(){
super.awakeFromNib()
//初始化代码
titleLabel.textColor=Constants.secondaryColor
textField.textColor=Constants.secondaryColor
contentView.isUserInteractionEnabled=false
errorLabel.isHidden=true
}
func setTag(标记:Int){
textField.tag=100+标记
}
}
然后在我的
FormViewController

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let field = formFields?[indexPath.row] else { return UITableViewCell() }        
    if let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as? RPTableViewCell {
        cell.titleLabel.text = field["displayText"]?.uppercased
        cell.textField.text = field["userAnswer"] as? String // This was missing, but is key
        cell.textField.placeholder = field["placeholder"] as? String
        cell.setTag(tag: indexPath.row)
        cell.textField.delegate = self
        return cell
    } else {
        return UITableViewCell()
    }
}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let cell = tableView.cellForRow(at: indexPath) as? RPTableViewCell else { return }
        tableView.scrollToRow(at: indexPath, at: .middle, animated: true)
        cell.textField.becomeFirstResponder()
    }

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if let nextTextField = tableView.viewWithTag(textField.tag + 1) as? UITextField {
            tableView.deselectRow(at: IndexPath(row: textField.tag - 100, section: 0), animated: false)
            tableView.selectRow(at: IndexPath(row: textField.tag - 99, section: 0),
                                animated: false, scrollPosition: .middle)
            nextTextField.becomeFirstResponder()
        } else {
            textField.resignFirstResponder()
        }
        return false
    }
编辑:

viewDidLoad
中,我像这样加载
表单

// Read from a JSON file
guard let visitorPaths = Constants.configDict()?["visitorPaths"] as? [Dictionary<String, AnyObject>] else {
        print("Error: no visitorPaths found in the configuration")
        return
    }
formFields = visitorPaths.first!["fields"]! as? [[String: AnyObject]]
//从JSON文件读取
guard let VisitorPath=Constants.configDict()?[“VisitorPath”]as?[词典]还有别的吗{
打印(“错误:在配置中找不到访问者路径”)
回来
}
formFields=visitorPaths.first![“字段”]!像[[String:AnyObject]]

您正在使用以下不起作用的代码段:

if let nextTextField = tableView.viewWithTag(textField.tag + 1) as? UITextField 
textfield不是tableView的子视图。Textfield是TableViewCell的子视图

您可以访问textFieldShouldReturn(textField:UITextField)委托方法中的单元格,如下所示:

let nextIndexPath = IndexPath(row: textField.tag + 1, section: 0)

if let cell = tableView.cellForRow(at: nextIndexPath) as? RPTableViewCell {
    cell.textField.becomeFirstResponder()
}
为文本跳转编辑: 添加以下textField委托方法来存储新文本:

func textFieldDidEndEditing(_ textField: UITextField) {
    formFields?[textField.tag - 100]["displayText"] = textField.text
}

这与tableView.dequeueReusableCells()的工作方式有关。在cellForRowAt()方法中,您没有设置textField的文本。因此,当单元格被重用时,它会显示以前的内容。我建议您为此使用静态表视图。谢谢您的评论。我将尝试以编程方式创建一个静态表,因为表单条目的数量(即行的数量)可能会改变。我尝试了这两种方法,但问题是相同的。我认为这两种方法是等效的。苹果在
viewWithTag
文档中说:“这个方法搜索当前视图及其所有子视图中指定的视图。”事实上,焦点跳到了正确的下一个文本字段。是上一个文本字段的文本跳转到其他地方。@JohnnyParafango您可以在formFields对象中添加设置文本的代码吗?我想您没有存储用户键入的新文本。请检查并尝试我的编辑是的,我知道我没有存储用户键入的文本,但您在编辑中编写的示例不适合我的情况。每个单元格由一个UILabel(
titleLabel=formFields[indexPath.row][“displayText”]
和一个空文本字段组成,如果存在占位符的话。因此
displayText
实际上是条目字段标题标签。但我可以尝试添加另一个键“answer”并将其设置为
textField.text
它可以工作!但是请像这样添加您的答案:在
tableView(\utableview:,cellForRowAt indexath:)
我应该添加类似于
cell.textField.text=formFields[indexath.row][“userAnswer”]
,其中开始时userAnswer明显为空或为零,然后,在编辑过程中,请将
displayText
替换为
userAnswer