15

I want to make a table view with textfields in each cell,

I have a custom class in a swift file:

import UIKit

public class TextInputTableViewCell: UITableViewCell{

    @IBOutlet weak var textField: UITextField!
    public func configure(#text: String?, placeholder: String) {
        textField.text = text
        textField.placeholder = placeholder

        textField.accessibilityValue = text
        textField.accessibilityLabel = placeholder
    }
}

Then in my ViewController I have

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let cell = tableView.dequeueReusableCellWithIdentifier("TextInputCell") as! TextInputTableViewCell

    cell.configure(text: "", placeholder: "Enter some text!")

     text = cell.textField.text

    return cell

}

That works well:

enter image description here

But when the user enters text in the textfield and press the button I want to store the strings of each textfield in an array. I have tried with

text = cell.textField.text
println(text)

But it prints nothing like if it was empty

How can I make it work?

Zablah
  • 332
  • 2
  • 3
  • 15
  • already answered in http://stackoverflow.com/questions/7344247/fetching-values-from-textfield-in-a-custom-cell-iphone – Hari Kunwar Jul 31 '15 at 01:59

4 Answers4

16

In your view controller become a UITextFieldDelegate

View Controller


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {

    var allCellsText = [String]()

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell

        cell.theField.delegate = self // theField is your IBOutlet UITextfield in your custom cell

        cell.theField.text = "Test"

        return cell
    }

    func textFieldDidEndEditing(textField: UITextField) {
        allCellsText.append(textField.text!)
        println(allCellsText)
    }
}

This will always append the data from the textField to the allCellsText array.

Linus
  • 423
  • 5
  • 12
Fred Faust
  • 6,696
  • 4
  • 32
  • 55
  • This works! But how can I append textfield.text to the array when the button is pressed? I tried putting: `allCellsText.append(textField.text) println(allCellsText)` inside the buttonPressed function but it doesnt identify the textfield – Zablah Jul 31 '15 at 03:14
  • That can't be done using it with the textField delegate, though you could set some type of condition on your data with the button, or copy the data to a different array. – Fred Faust Jul 31 '15 at 03:18
  • @thefredelement In `class ViewController` I have created an instance of TextInputTableViewCell and in ViewDidAppear, I am trying to assign the data from UserDefaults to `var textField` from TextInputTableViewCell, but the assignment fails, unexpectedly found nil while unwrapping an Optional value – bibscy Mar 27 '17 at 04:03
  • 1
    But won't this just keep on appending to `allCellsText` whenever `textFieldDidEndEditing` is called? – aheze Oct 03 '20 at 21:42
2

this method is init the cell,u have not model to save this datas,so

text = cell.textfield.text

is nothing! you can init var textString in viewcontroller,an inherit UITextFieldDelegate

optional func textFieldDidEndEditing(_ textField: UITextField)
{
     textString = _textField.text
}
optional func textFieldShouldReturn(_ textField: UITextField) -> Bool{
    return true
}

and cell.textField.text = textString

2

create a variable

var arrayTextField = [UITextField]()

after this in your func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{} add

self.arrayTextField.append(textfield)

before returning cell, after this to display the values using

for textvalue in arrayTextField{
        println(textvalue.text)
    }
knownUnknown
  • 869
  • 11
  • 18
  • 2
    Wouldn't this cause issues for table rows that might become recycled, or does it retain the memory location of the cell even if you scroll out of view? – Hedylove Mar 18 '18 at 22:34
  • This is a good idea, however as @JoseRamirez pointed out, if you scroll the view, your cellForRowAtIndexPath will append another textview. – justdan0227 Feb 07 '20 at 00:42
-1

So here is a way to do it using the suggestion of having a textfield array. It uses the tag of the textfield to ensure it does not get added multiple times when you scroll.

// Assumes that you have a label and a textfield as a part of you tableViewCell

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? BodyStatsTableViewCell else {
        return UITableViewCell()
    }
    cell.rowLabel.text = bodyTableArray[indexPath.row].rowTitle
    let tagValue = indexPath.row + 100
    var newRow = true
    for textvalue in arrayTextField{
        if textvalue.tag == tagValue {
            newRow = false
        }
    }
    if newRow {
        cell.rowTextField.tag = tagValue
        self.arrayTextField.append(cell.rowTextField)
        cell.rowTextField.text = bodyTableArray[indexPath.row].rowValue
    }
    return cell
}

// And then you cam get the values when you want to save with where tag - 100 will be the array index value to update

for textvalue in arrayTextField{ print(textvalue.text) }

justdan0227
  • 1,374
  • 18
  • 48
  • 1
    After scrolling, this may have different UITextFields in the `arrayTextField` than are visible in the UI. Also, the other solutions to the question are working correctly without having to resort to this kind of hacks. – fishinear May 28 '21 at 16:34
  • @fishinear, thanks, not sure where I was going with this – justdan0227 Aug 02 '21 at 12:15