UITextField not editable and not showing keyboard

情到浓时终转凉″ 提交于 2019-12-11 17:12:48

问题


I am using a table view controller to display content. I am trying to make an editable UITextField, but it is not editable. I can get them to display in each cell, but when I tap on them they aren't editable. I am also trying to store what the user enters in each cell. Please ignore the commented out parts. Here is my code:

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
}

回答1:


Changecell.addSubview(sampleTextField) to cell.contentView.addSubview(sampleTextField)

Please refer to https://developer.apple.com/documentation/uikit/uitableviewcell/1623229-contentview for details about contentView




回答2:


There could be couple of reasons for textfield is not editable .

  1. Check if you have used the UITextFieldDelegate and set the textfield delegate to self i.e. textfield.delegate = self

  2. When you add subviews to cell make sure you add it to the content view of the cell. e.g cell.contentView.addSubview(textField)

  3. Make sure there is no other view on top of textfield.

  4. If you have implemented :

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool then it should return true otherwise keyboard will not appear and you won't be able edit text field.

  1. Check if textfield is enabled both view swift file and storyboard .

6.Make sure proper connection is made between textfield in your storyboard or Xib file and @IBOutlet textfield in your view controller where you have declared textfield.

  1. please check that you are not resigning keyboard or endEditing is set to true on textFieldShouldBeginEditing, shouldChangeCharactersIn and textFieldShouldBeginEditing delegates of textfield.

I hope this helps you out!!




回答3:


According to me,

  1. You need to set priority of sampleTextField upon tableview cell selection. Whenever you are trying to select the sampleTextField to edit it, didselect function of tableview is getting call. Check by putting breakpoints in didselect function. If this is happening, you need to set the priority.
  2. Also, check if you are not resigning keyboard in textFieldShouldEndEditing in delegate function of keyboard.
  3. sampleTextField.isUserInteractionEnabled = true
  4. Also, add sampleTextField on cell.contentView instead directly to cell.

Once, You are able to edit, you can directly get the data from the textfield for storing it by sampleTextField.text

Hope, this will be effective. If you still find issue, feel free to ask.



来源:https://stackoverflow.com/questions/48798239/uitextfield-not-editable-and-not-showing-keyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!