【发布时间】:2018-02-15 00:06:10
【问题描述】:
我正在使用表格视图控制器来显示内容。我正在尝试制作一个可编辑的 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
}
【问题讨论】:
-
您似乎没有遵循您在your previous question 上收到的任何好的建议。
-
sampleTextField.isUserInteractionEnabled = true
-
如果我包含完整代码而不只是这部分代码会有帮助吗?其他地方可能有问题。
标签: ios swift uitableview uitextfield