How to get tableview cell's textfield values in submit button in swift

57 Views Asked by At

I have added textfield in tableview cell and want all textfield values in submitButton

code: how do i get added tableview text in submitButton

class TableCell: UITableViewCell{
@IBOutlet weak var tableLbl: UILabel!
@IBOutlet weak var textFieldData: UITextField!
}

class TableviewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

var selectedRows = [Int]()
var textfieldDataArray = [String]()
@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableCell", for: indexPath) as! TableCell
    
    cell.textFieldData.delegate = self
    cell.textFieldData.tag = indexPath.row
    
    cell.textFieldData.addTarget(self, action: #selector(textfieldDidChange(_:)), for: .editingChanged)
    
    cell.tableLbl.text = String(indexPath.row + 1)
    return cell
}

@objc func textfieldDidChange(_ textField: UITextField) {
    let tag = textField.tag
    textfieldDataArray[tag] = textField.text ?? ""
}
@IBAction func submitButton(_ sender: UIButton) {
    var selectedTextfieldValues = [String]()
    
    print("Selected Textfield Values: \(selectedTextfieldValues)")
    // Use selectedTextfieldValues as needed
}
}

got error at textfieldDataArray[tag] = textField.text ?? ""

Thread 1: Fatal error: Index out of range

1

There are 1 best solutions below

2
son On

I think the better approach is defining a cell data to hold String value, it is safer and easier to handle than assigning tag and reusing cell at the same time.

It could be:

struct TableCellData {
    var text = ""
}

class TableCell: UITableViewCell {
    ...
    var changedText: ((String) -> Void)?

    override func awakeFromNib() {
        super.awakeFromNib()
        textFieldData.addTarget(self, action: #selector(textfieldDidChange), for: .editingChanged)
    }
    
    func bind(_ data: TableCellData) {
        textFieldData.text = data.text
    }   

    @objc private func textfieldDidChange(_ textField: UITextField) {
        changedText?(textField.text ?? "")
    }
}

Then when dequeuing those cells:

class TableviewViewController {

    //How many cell you want
    private var list: [TableCellData] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        list = Array(repeating: TableCellData(), count: 10)
    }    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        cell.bind(list[indexPath.row])
        cell.changedText = { [weak self] text in
            guard let self = self else { return }
            self.list[indexPath.row].text = text
        }
        return cell
    }
    
    private func getAllString() {
        let strings = list.map { $0.text }
        print(strings)
    }
}