0
votes

I am trying to delete a cell on a tableview that contains sections and this is the error I get:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3318.93/UITableView.m:1582

The code I am using is:

override func tableView(tableView: UITableView?, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath?) {

    if (editingStyle == UITableViewCellEditingStyle.Delete) {

        let cell = tableView?.cellForRowAtIndexPath(indexPath!)
        let staffNic = cell?.textLabel?.text as String!

        var salidaStaff:PFQuery = PFQuery(className: "StaffTrabajo")
        salidaStaff.whereKeyDoesNotExist("Salida")
        salidaStaff.whereKey("Nic", equalTo: staffNic)
        salidaStaff.findObjectsInBackgroundWithBlock {
            (objects:[AnyObject]!, error:NSError!)->Void in
            if error == nil{
                for object in objects{
                    let sweet:PFObject = object as PFObject

                    // Busca el id del staff
                    let iddd:String! = sweet.objectId as String!

                    // guarda los datos de salida
                    var query = PFQuery(className:"StaffTrabajo")
                    query.getObjectInBackgroundWithId(iddd) {
                        (gameScore: PFObject!, error: NSError!) -> Void in
                        if error != nil {
                            println(error)
                        } else {
                            gameScore["Salida"] = NSDate()
                            gameScore.saveInBackground()
                                                            tableView?.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: UITableViewRowAnimation.Automatic)
                        }
                        self.tableView.reloadData()
                    }
                }

            }
        }
    }
}

Any ideas? This is how I have my datasource setup:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.aventurasUnicas.count
}


override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "\(self.aventurasUnicas.objectAtIndex(section))"
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var sectionTitle:String = aventurasUnicas.objectAtIndex(section) as String
    var sectionStaff:Array = porAventura[sectionTitle]!
    return sectionStaff.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell;

    // Obtiene el nombre del staff y lo pone en la celda
    var sectionTitle:String = aventurasUnicas.objectAtIndex(indexPath.section) as String
    var sectionStaff:Array = porAventura[sectionTitle]!
    let staffNic:String = sectionStaff[indexPath.row] as String
    cell.textLabel?.text = staffNic

    // Obtiene el subtitulo
    var subtituloAventura:String = "\(sectionTitle).\(staffNic)" as String
    var sectionStaff2:Array = subtituloTabla[subtituloAventura]!
    let staffNic2:String = sectionStaff2[0] as String
    cell.detailTextLabel?.text = staffNic2

    cell.accessoryType = UITableViewCellAccessoryType.DetailButton
    return cell
    }
1
When? All the time? Only when deleting the last item in a section? Be specific. - Wain

1 Answers

0
votes

Changes to a table view must be performed in concert with changes to the table view's datasource. So before calling deleteRowsAtIndexPaths, you must first modify the array that backs the table.

Hopefully, in numberOfRowsInSection the code refers to an array (call it model) and answers model.count. Hopefully also, the indexPath.row where you ask the table to delete is a valid index into model (< model.count). Before calling deleteRowsAtIndexPaths, delete the item in model at indexPath.row.