我正在使用 TableView Controller 来显示内容。我正在尝试制作一个可编辑的 UITextField,但它不可编辑。我可以让它们显示在每个单元格中,但是当我点击它们时,它们是不可编辑的。我还试图存储用户在每个单元格中输入的内容。请忽略注释掉的部分。这是我的代码:
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return words.count
}
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:60, y: 30, width: 150, height: 40))
sampleTextField.placeholder = "Enter text here"
sampleTextField.font = UIFont.systemFont(ofSize: 15)
sampleTextField.borderStyle = UITextBorderStyle.roundedRect
sampleTextField.autocorrectionType = UITextAutocorrectionType.no
sampleTextField.keyboardType = UIKeyboardType.default
sampleTextField.returnKeyType = UIReturnKeyType.done
sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
sampleTextField.delegate = self as? UITextFieldDelegate
var userEntry = sampleTextField.text
sampleTextField.tag = indexPath.item // You will access the text field with this value
cell.addSubview(sampleTextField)
//cell.textLabel?.text = shuffledWord
//var imageName = UIImage(named: words[indexPath.row])
//cell.imageView?.image = imageName
return cell
}
最佳答案
将cell.addSubview(sampleTextField)
更改为cell.contentView.addSubview(sampleTextField)
请引用https://developer.apple.com/documentation/uikit/uitableviewcell/1623229-contentview有关 contentView
关于ios - UITextField 不可编辑且不显示键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48798239/