Reputation: 21
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
}
Upvotes: 1
Views: 2709
Reputation: 579
According to me,
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.
Upvotes: 0
Reputation: 7732
There could be couple of reasons for textfield is not editable .
Check if you have used the UITextFieldDelegate and set the textfield delegate to self i.e. textfield.delegate = self
When you add subviews to cell make sure you add it to the content view of the cell. e.g cell.contentView.addSubview(textField)
Make sure there is no other view on top of textfield.
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.
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.
I hope this helps you out!!
Upvotes: 3
Reputation: 1297
Changecell.addSubview(sampleTextField)
to cell.contentView.addSubview(sampleTextField)
Please refer to https://developer.apple.com/documentation/uikit/uitableviewcell/1623229-contentview for details about contentView
Upvotes: 8