![](/img/trans.png)
[英]UITextField inside UITableviewcell. editable but keyboard is not showing
[英]UITextField not editable and not showing keyboard
我正在使用表視圖控制器來顯示內容。 我試圖制作一個可編輯的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)
有關contentView
詳細信息,請參閱https://developer.apple.com/documentation/uikit/uitableviewcell/1623229-contentview
文本字段不可編輯可能有兩個原因。
檢查您是否使用過UITextFieldDelegate並將文本字段委托設置為self,即textfield.delegate = self
將子視圖添加到單元格時,請確保將其添加到單元格的內容視圖中。 例如cell.contentView.addSubview(textField)
確保在文本字段頂部沒有其他視圖。
如果已實施:
func textFieldShouldBeginEditing(_ textField:UITextField)-> Bool,則應返回true,否則鍵盤將不會出現,並且您將無法編輯文本字段。
6.確保在情節提要或Xib文件中的文本字段與聲明了文本字段的視圖控制器中的@IBOutlet文本字段之間建立了正確的連接。
我希望這可以幫助你!!
據我說
一次,您就可以進行編輯,您可以直接從文本字段中獲取數據以通過sampleTextField.text進行存儲。
希望這會有效。 如果您仍然發現問題,請隨時提出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.