duck
duck

Reputation: 415

Why do I get an EXC_BAD_ACCESS Error when deleting values from a UITableView?

I am creating an app where I have data stored in a UITableView inside a view controlled by a class I have created, TeamsViewController. I have code to delete values from the table, but sometimes (not always), the app will crash when you delete values from the table view.

I get the error Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) on the class declaration of my AppDelegate:

class AppDelegate: UIResponder, UIApplicationDelegate

Here is the code for TeamsViewController:

@IBOutlet weak var table: UITableView!
var data: [Team]()

override func viewDidLoad()
{
    super.viewDidLoad()

    if(table != nil)
    {
        self.table.delegate = self
        self.table.dataSource = self
        self.table.reloadData()
    }
}
func tableView(tableView: UITableView!, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath!)
{
    println("1234")
    if (editingStyle == UITableViewCellEditingStyle.Delete)
    {
        data.removeAtIndex(indexPath.row)
        table.reloadData()
    }
}

Note that Team is a class that I have created .

I have omitted some of the tableView code, tell me if it is needed and I will add it.

Some more information that might be helpful is that "1234" is NOT printed to the console when the app crashes.

Thank you in advance.

Upvotes: 1

Views: 773

Answers (1)

Kelvin Lau
Kelvin Lau

Reputation: 6781

I'm not certain what error it is, but based on a tutorial I did on tableViews, their swipe-to-delete function override contained this line:

tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)

Maybe adding that line would clear your error.

Upvotes: 2

Related Questions