Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Swipe 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

Hello,

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.

Thanks Jhoan, I am looking into how to do this. I appreciate your help.