leaner122
leaner122

Reputation: 657

How to save reorder of rows in table view?

I am able to reorder the rows in tableview and navigate through app and my selection remains intact but when I restart the simulator the reorder is gone. Below is my code. How do I fix this.

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let cityLabel: String = cityName[sourceIndexPath.row]
    cityName.remove(at: sourceIndexPath.row)
    cityName.insert(goalToMoveLabel, at: destinationIndexPath.row)
      }

Upvotes: 0

Views: 357

Answers (1)

Glenn Posadas
Glenn Posadas

Reputation: 13291

First thing came to my mind when I read your question is to save the data sources either in a database (Realm or CoreData or MySQLite or whatnot). You can also save your data sources in User Defaults.

Make sure when re-ordering the views/data, you are re-ordering your data source as well.

Then inside viewDidLoad, fetch the data sources, in your case, it's cityName.

After that, call self.tableView.reloadData().

Upvotes: 2

Related Questions