0

My first line of writing Copy itself to the bottom lines

And when I scroll, Places of data are changing

img1

img2

legoscia
  • 39,593
  • 22
  • 116
  • 167
  • 2
    It's because your cells are being reused. The cells that had the values `1, 2, 3` are now being reused, and they still have the values that were saved in them the first time. If you retain the values from the previous entries, save them separately and while returning cells in `cellForRowAtIndexPath`, check if you have already saved values there or not. – Shamas S May 02 '17 at 08:55
  • add your code of cellForRowAtIndexPath method – Priyal May 02 '17 at 08:57
  • My answer here has a detailed explanation: http://stackoverflow.com/a/43164656/2179970 – Zion May 02 '17 at 09:17
  • Zion Perez I did No longer copied but the data is lost –  May 02 '17 at 09:33

5 Answers5

0

Data in UITableView Cell is repeated because Cell is reused so you will need to keep track of data for cell, may be you can add data in array arranged by index.

Abhishek
  • 509
  • 3
  • 12
0

Yes thats the concept of UITableview dequeCells: as it reuses cells , so if there are say 100 entries only limited number of cells will be created at a time in order to save memory. Now in order to avoid it, fill your cell views with datasource that has all the values. If you see that some values are missing from datasource, just use, for example:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Tek101TableViewCell
    cell.selectionStyle = .none
    cell.siraLabel.text = ""
    cell.siraLabel.text = String(indexPath.row + 1) + ")"
    return cell
}
ankit
  • 3,537
  • 1
  • 16
  • 32
0
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Tek101TableViewCell

    cell.selectionStyle = .none

    cell.siraLabel.text = String(indexPath.row + 1) + ")"



    return cell
}
0

You need to set value in textfield delegate method as below :(As per your need)

If you are use return button in keyboard

       func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            let cell = textField.superview?.superview as! Tek101TableViewCell // track you view hierarchy
            cell. siraLabel?.text = textField.text
            textField.resignFirstResponder()
            return true
        }

If you want to track value end editing method

func textFieldDidEndEditing(_ textField: UITextField){
    let cell = textField.superview?.superview as! Tek101TableViewCell // track you view hierarchy
    cell. siraLabel?.text = textField.text
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

In UITablview cell is reused so this thing will happen. To solve this issue you have to keep all the records inside one global array variable.

You can store all input in array or dictionary while user insertion. and then you have to give this array values to each and every cell from CellForRow.

In short you have to deal with array only.

Hope this will help you.

Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
  • No... don't write like this directly. But write like, `cell.Textfield.text = sell.myArray[0] as! String` This is for example – Jigar Tarsariya May 02 '17 at 09:23
  • before this you must have to create array with actual values. If there is no value then add default value in that array. then assign this array in cellforrow. – Jigar Tarsariya May 02 '17 at 09:24
  • var myTexts: [String?]! func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { print("CellForRowAt: " + indexPath.row.description) let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Tek101TableViewCell cell.oyuncu1TextField.text = myTexts[indexPath.row] return cell } –  May 02 '17 at 09:31