簡體   English   中英

UITextField不可編輯,並且不顯示鍵盤

[英]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

文本字段不可編輯可能有兩個原因。

  1. 檢查您是否使用過UITextFieldDelegate並將文本字段委托設置為self,即textfield.delegate = self

  2. 將子視圖添加到單元格時,請確保將其添加到單元格的內容視圖中。 例如cell.contentView.addSubview(textField)

  3. 確保在文本字段頂部沒有其他視圖。

  4. 如果已實施:

func textFieldShouldBeginEditing(_ textField:UITextField)-> Bool,則應返回true,否則鍵盤將不會出現,並且您將無法編輯文本字段。

  1. 檢查是否已啟用文本字段,同時查看swift文件和Storyboard。

6.確保在情節提要或Xib文件中的文本字段與聲明了文本字段的視圖控制器中的@IBOutlet文本字段之間建立了正確的連接。

  1. 請檢查您是否不辭職鍵盤,或者textFieldShouldBeginEditing,shouldChangeCharactersIn和textFieldShouldBeginEditing文本字段的代表的endEditing設置為true。

我希望這可以幫助你!!

據我說

  1. 您需要在選擇表格視圖單元格時設置sampleTextField的優先級。 每當您嘗試選擇sampleTextField對其進行編輯時,都會調用tableview的didselect函數。 通過將斷點放在didselect函數中進行檢查。 如果發生這種情況,則需要設置優先級。
  2. 另外,請檢查是否在鍵盤的委托功能的te​​xtFieldShouldEndEditing中退出鍵盤。
  3. sampleTextField.isUserInteractionEnabled = true
  4. 另外,直接在cell.contentView上添加sampleTextField而不是直接添加到cell。

一次,您就可以進行編輯,您可以直接從文本字段中獲取數據以通過sampleTextField.text進行存儲。

希望這會有效。 如果您仍然發現問題,請隨時提出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM