Swift tableview changes the value of a random cell when one cell is edited

前端 未结 2 1879
我在风中等你
我在风中等你 2021-01-28 15:10

I have a long list of textfields so I am using a tableView.

this is how the screen looks like

When I insert some text in a textfield in one of the cells and scro

相关标签:
2条回答
  • 2021-01-28 15:46

    Your main problem is that you are keeping your data inside Views (UITableVieCell).

    Cells are reused by UITableView to optimize performance - so even if you have 1milion rows in your table, only few UITableViewCells are in memory (as many as are visible on the screen usually).

    When you are calling dequeueReusableCell it takes one of already existing cells and reuse it.

    To solve this problem you need to keep your data separately in an Array and keep modified texts there. Then you need to modify code you posted, to take data every single time you configure UITableView from your "dataSource".

    Also a good pratcice is to reset UITableViewCell when it's reused, you can do this by adding coding inside prepareForReuse - but that's optional, as text will be set from your data source anyways.

    I would also recommend to read this before you start coding further - it will allow to understand how UITableView works on iOS:

    https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW1

    0 讨论(0)
  • 2021-01-28 15:57

    Basically, you have to get and store the values in view-controller because due to the reusable behavior of UITableViewCell you lost the reference of the invisible cell with all the child reference.

    So you can store the text-field value by the textViewDidChange action in RelatieToevoegenCell class.

    0 讨论(0)
提交回复
热议问题