moxmlb
moxmlb

Reputation: 1330

How to get TextField Input and why the input repeat when I scroll?

I create TableViewCells like this:

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ErnaehrungTableViewCell", for: indexPath) as! ErnaehrungTableViewCell
    let row = indexPath.row
    cell.LabelType.text = cellValues[indexPath.row]
    cell.TextValue.keyboardType = .decimalPad
    cell.TextValue.tag = indexPath.row

    cell.selectionStyle = .none
    cell.ButtonUnit.addTarget(self, action: #selector(ErnaehrungManualController.ButtonUnitClick), for: .touchUpInside)
    cell.ButtonUnit.tag = indexPath.row`

LabelType ist a Label, TextValue a Textfield and ButtonUnit is a Button. It looks like this: enter image description here

If I type in at the first five TextFields (as shown) and I scroll, the inputs are in other rows, too.

How can I fix that?

If I press the Button at the upper right (Fertig) then I want to read the text of all TextFields. I have this code, but I doesn't work:

for var i in 0...cellValues.count-1 {
        let indexPath = IndexPath(row: i, section: 0)
        let cell = tableView.dequeueReusableCell(withIdentifier: "ErnaehrungTableViewCell", for: indexPath) as! ErnaehrungTableViewCell

        if let text = cell.TextValue.text, !text.isEmpty
        {
            let textString = cell.TextValue.text!
            print(textString)

        }
    }

So how can I fix that the TextFields (where I not typed in a text) don't copy the input of other TextFields?

And how I can read out the TextField.text for all rows?

Thanks for help, if you need more code, I will add that.

Upvotes: 1

Views: 1655

Answers (1)

Lineesh K Mohan
Lineesh K Mohan

Reputation: 1712

This is because of dequeue the cell which reuses the cell . In order to avoid this issue, store the value in an array and then set the value when table view delegate is called. In SWIFT 3

var dataList = ["","","",""] // Array size should be same as the total row count


func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { 

    if textField.text != ""{

        dataList[textField.tag] = textField.text!
    }
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "ErnaehrungTableViewCell", for: indexPath) as! ErnaehrungTableViewCell
    let row = indexPath.row
    cell.LabelType.text = cellValues[indexPath.row]
    cell.TextValue.text = dataList[indexPath.row] // Setting the value here
    cell.TextValue.keyboardType = .decimalPad
    cell.TextValue.tag = indexPath.row
    cell.TextValue.delegate = self // Setting delegate here
    cell.selectionStyle = .none
    cell.ButtonUnit.addTarget(self, action: #selector(ErnaehrungManualController.ButtonUnitClick), for: .touchUpInside)
    cell.ButtonUnit.tag = indexPath.row`

}

Upvotes: 6

Related Questions