UITableView
by default reuse cells. But in my case some cells have a UITextField
object and I don't want to loose its value when the cell is reused. The table has few rows so I can handle all the cells in memory.
Example of code I currently use:
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "TPAddressInfoTableViewCell", bundle: nil), forCellReuseIdentifier: InfoCellIdentifier)
...
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.row]
switch item.appearance {
case .textWithTitle:
let cell = tableView.dequeueReusableCell(withIdentifier: InfoCellIdentifier) as! TPAddressInfoTableViewCell
return cell
case .picker:
...
How to retain such cells correctly? Linking UITextFieldDelegate
of each cell to current UIViewController
seems to be extra in this case.
Don't retain cells .
Save all values of the UI elements of the cell in the data model and restore the values in cellForRow
.
For example pass the corresponding model item to the cell in cellForRow
and save the text field string in a property when it changes.
Alternatively use a callback closure to update the property in the controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.