Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLee Weiss
1,079 PointsSwipe delete not saving/updating.
Hello all,
I would love a little assistance, if possible. I have successfully added the swipe delete function into a tableView and at first glance, it works properly. However, when I reload the app or add another entry (cell) to the table, all of the deleted data reappears. I have posted my code below as a reference. I am aware that the swipe delete function should be an "override func" but when I add the override I get the error "Method does not override any method from its superclass."
Thanks for the help -Lee
Code:
extension ViewController: UITableViewDelegate {
// didselectrowat...
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let vc = storyboard?.instantiateViewController(identifier: "task") as! TaskViewController
vc.title = "New Entry"
vc.entry = myEntry[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
}
}
extension ViewController: UITableViewDataSource {
// numberofrows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myEntry.count
}
// Slide delete
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
self.myEntry.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
}
}
1 Answer
Jhoan Arango
14,575 PointsHello,
If you hardcoded the datasource for the tableview, then this will be normal behavior. What you want to do is create a way to save the state of your data source. There are several ways of doing this, using "Userdefaults", or a library such as "Realm", or even Apple's Core Data. I personally recommend "Realm".
Remember that when you inherit from a super class then you use the override, but in this case you are not inheriting from "UITableViewController", instead you are using an instance of "Table View" and conforming to its DataSource, and Delegate", so the override is not happening.
Hope this helps, and let me know if you need more assistance.
Lee Weiss
1,079 PointsLee Weiss
1,079 PointsThanks Jhoan, I am looking into how to do this. I appreciate your help.