Ios 用户输入不处理以编程方式在表视图中添加的文本字段
我正在尝试添加一个用户可编辑的文本字段。我能够让文本字段为表中的每个不同单元格显示,但无法让用户输入。它没有显示键盘。我启用了用户交互。这是我的密码。请忽略注释掉的部分Ios 用户输入不处理以编程方式在表视图中添加的文本字段,ios,swift,uitableview,uitextfield,Ios,Swift,Uitableview,Uitextfield,我正在尝试添加一个用户可编辑的文本字段。我能够让文本字段为表中的每个不同单元格显示,但无法让用户输入。它没有显示键盘。我启用了用户交互。这是我的密码。请忽略注释掉的部分 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentif
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "transportCell", for: indexPath)
//self.tableView.rowHeight = 100
//var a = Array(words[indexPath.row])
//a.shuffle()
//var shuffledWord = String(a)
//shuffledWord = shuffledWord.lowercased()
let sampleTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
sampleTextField.placeholder = "Enter text here"
sampleTextField.font = UIFont.systemFont(ofSize: 15)
sampleTextField.borderStyle = UITextBorderStyle.roundedRect
sampleTextField.autocorrectionType = UITextAutocorrectionType.no
sampleTextField.keyboardType = UIKeyboardType.alphabet
sampleTextField.returnKeyType = UIReturnKeyType.done
sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
sampleTextField.delegate = self as? UITextFieldDelegate
// cell.textLabel?.text = shuffledWord
// var imageName = UIImage(named: words[indexPath.row])
//cell.imageView?.image = imageName
return cell
}
如果可能的话,您能告诉我如何将用户输入存储为变量吗
提前谢谢,正如@rmaddy所说,首先,您必须将该文本字段添加到您的单元格中,以便
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {......
sampleTextField.text = arrTextFieldVal[indexPath.item]//this array for stroing the user input values..
sampleTextField.tag = indexPath.item // u will access the text field with this value
cell.addSubview(sampleTextField)........}
其次,您可以通过管理字符串数组来存储用户值,如
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrTextFieldVal.count }
并从文本字段中获取值,如下所示
func textFieldDidEndEditing(_ textField: UITextField) {
arrTextFieldVal[textField.tag] = textField.text!
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
请参阅,看看它是否对您有帮助
- 此外,请检查您是否正在退出textfield ShouldBeginediting、ShouldChangeCharacters in或textFieldShouldBeginEditing textfield代理上的键盘
cellForRowAt
。单元格将被重用。您将使用下面的代码向每个单元格添加越来越多的文本字段。Benjamin Okeefe学生使用带有文本字段的自定义单元格,否则问题将如@rmaddy所述。