首页 > 解决方案 > 表格视图中的文本字段被清除

问题描述

我目前正在创建一个应用程序和 atm 我正在登录和注册功能。我决定创建一个表格视图,其中包含文本字段,以便用户可以注册。但是每次我在文本字段中输入内容并向下滚动时,文本字段都会被清除,但是为什么呢?

另外,当我点击一个按钮时,如何将这些数据添加到我的数据库中?因为我无法从那里到达它。有输入吗?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "signUpCell", for: indexPath) as! SignUpTableViewCell

    cell.txtfield.delegate = self
    cell.textOfField.text = textNames[indexPath.section][indexPath.row]
    cell.txtfield.text = textFields[indexPath.section][indexPath.row]


@IBAction func signUpButtonTapped(_ sender: Any)
{
    let authentication = Auth.auth().currentUser?.uid
    //I know how to add it to the database, but how can I retrieve the tableview data here?

}

标签: iosswiftuitableview

解决方案


单元格出队需要更新数据源数组,这在里面cellForRowAt

 cell.textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
 cell.textField.indexPath = indexPath

@objc func textFieldDidChange(_ textField: CustomTexf) { 
   let index = textField.indexPath
   textFields[index.section][index.row] = textField.text!
}

创建此类并将其分配给文本字段

class CustomTexf : UITextField {
   var indexPath:IndexPath!
}

推荐阅读