How to make UITableview with Textfield in swift?

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.


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