25406

UITextField not editable and not showing keyboard

Question:

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 }

Answer1:

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

Please refer to <a href="https://developer.apple.com/documentation/uikit/uitableviewcell/1623229-contentview" rel="nofollow">https://developer.apple.com/documentation/uikit/uitableviewcell/1623229-contentview</a> for details about contentView

Answer2:

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

<ol><li>

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

</li> <li>

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

</li> <li>

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

</li> <li>

If you have implemented :

</li> </ol>

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

<ol start="5"><li>Check if textfield is enabled both view swift file and storyboard .</li> </ol>

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.

<ol start="7"><li>please check that you are not resigning keyboard or endEditing is set to true on textFieldShouldBeginEditing, shouldChangeCharactersIn and textFieldShouldBeginEditing delegates of textfield.</li> </ol>

I hope this helps you out!!

Answer3:

According to me,

<ol><li>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.</li> <li>Also, check if you are not resigning keyboard in textFieldShouldEndEditing in delegate function of keyboard.</li> <li>sampleTextField.isUserInteractionEnabled = true</li> <li>Also, add sampleTextField on cell.contentView instead directly to cell.</li> </ol>

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.

Recommend

  • Random order of buttons
  • Python: select random values for a column from csv
  • Why is Math.random ignoring the last element? [duplicate]
  • UILabel and UITextView line breaks don't match
  • Installing NuGet package when dependency has been renamed
  • print an array in random order [duplicate]
  • The #selector is not compatible with the closure?
  • Adding multiple pictureboxes to a form programmatically in vb.net ?
  • UITextfield not editable-iphone [duplicate]
  • html/hta mutiple file in audio player
  • passing data between view controllers programmatically Swift
  • In Swift, how can I change an attribute of part of a string?
  • Create Vertical lines between Uilabels inside of a stack view [duplicate]
  • How to create shuffled batch list from numpy arrays to feed tensorflow dictionnary
  • Custom UITableViewCell in UISearchDisplayController
  • How do I know if text exceeds the bounds of a text view?
  • Resizing UITableViewCells in iOS4 versus iOS3
  • How to stop the horizontal scrolling programmatically?
  • How to check does method systemFontOfSize(fontSize: weight:) exist in iOS
  • How to override UserControl class to draw a custom border?
  • Yii2: Using Kartik Depdrop Widget?
  • How to Add Polymorphic Comments to Feed?
  • (Tcl/Expect) clear screen after exit
  • Xaml, wpf image position and crop issue
  • Scanner nextInt() and hasNextInt() problems
  • Getting different value with placeholder over CPU/GPU
  • jQuery: How to AJAXify WordPress Search?
  • Trying to get the char code of ENTER key
  • D3 get axis values on zoom event
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Xamarin Forms - UWP Fonts
  • How to set/get protobuf's extension field in Go?
  • MySQL WHERE-condition in procedure ignored
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Change an a tag attribute in JavaScript based on screen width
  • How to show dropdown in excel using jrxml (jasper api)?
  • Web-crawler for facebook in python
  • Unanticipated behavior
  • trying to dynamically update Highchart column chart but series undefined
  • java string with new operator and a literal