How to make UITableview with Textfield in swift?

Related searches

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:

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?

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.

How to make UITableview with Textfield in swift?, In the TableTableViewController.swift -> cellForRow Note: Don't forget to make tableView weak in this closure's capture list because you When there is a UITextView or a UITextField inside a UIScrollView, the scrollview� Swift UITableView Tutorial with Custom Cells - Beginner Series - Duration: 26:35. Sean Allen 142,855 views. 26:35. UITableView - Create Custom Table View Cell - Duration: 36:19.

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)
    }

Embedding UITextView inside UITableViewCell, For those reasons, I think a table view is a really convenient choice for collecting data from a Create an IBOutlet between the text field and the� Step 1: Create a ViewController.swift file. This will serve as the View Controller where we add our tableView subview and manage the UIKit app’s interface. class ViewController: UIViewController {} Step 2: In our ViewController class, instantiate a UITableView object

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

Custom UITableViewCell for Text Input in Swift, Displaying static text in UITableView is easy but for enabling text input in UITableView and TextInputFormItem represents a single row with editable text field. UITableViewCellTextInput (GitHub) Xcode 10.1, Swift 4.2.1� You can do it as, [code]func getTextFeildValuesFromTableView() ->[String:String?]?{ var values = [String:String?]() for (index, value) in placeholders.enumerated

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) }

Text input in UITableView – Augmented Code, Change the height value to 60 . Add the UITextField and UIButton to the view. Now that we have updated the size of our view, we can add a� Show activity on this post. This should not be difficult. When creating a cell for your table, add a UITextField object to the cell's content view. Set the delegate of the UITextField as self (ie your viewcontroller) Give a tag to the text field so you can identify which textfield was edited in your delegate methods.

Create a Custom UITableViewCell with Swift, The best working Solution of adding Custom header view in UITableView for section in swift 4 is -- Updated for swift 3.0. Step 1. Create

Select the new UITableViewCell (shows up as “Table View Cell” in the Document Outline). In the Attributes Inspector, change it’s Style to “Basic”, and give it the Identifier “TextCell”. Open the Assistant Editor, and Ctrl+Drag from your UITableView to somewhere in your code.

This video covers creating custom text field backgrounds using sliced images. How To Create A TableView In Xcode 8 (Swift 3.0) - Duration: 6:55. The Swift Guy 116,138 views.

Comments
  • already answered in stackoverflow.com/questions/7344247/…
  • 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
  • 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.
  • @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
  • 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?
  • This is a good idea, however as @JoseRamirez pointed out, if you scroll the view, your cellForRowAtIndexPath will append another textview.