删除UITableView的最后一行时崩溃

时间:2015-09-16 15:55:17

标签: swift uitableview lldb

当我删除UITableView的最后一行时,我收到一个没有其他消息的lldb错误。这是我的代码:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]?  {
    if tableView == sendTableView {
        return []
    } else {
        //delete row
    let deleteRow = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        self.oId = globalClass.messageObjects[indexPath.row].objectId!!
        self.messageToDelete = globalClass.messageObjects[indexPath.row]["fromFriend"] as! String
        globalClass.messageObjects.removeAtIndex(indexPath.row)
        self.myMap.removeAnnotation(self.pinAnnotationArray[indexPath.row])
        self.pinAnnotationArray.removeAtIndex(indexPath.row)
        self.messageTableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
        self.deleteMessage()
    })
        //get directions
...
        return [deleteRow,getDirections]
    }
}


//delete message
func deleteMessage() {
        _ = PFQuery(className:"Messages")
        let query = PFQuery(className:"Messages")
        query.getObjectInBackgroundWithId(oId){ (object: PFObject?, error: NSError?) -> Void in

            if error == nil && object != nil {
                    if self.messageToDelete == PFUser.currentUser()?.username {
                        object!.deleteInBackgroundWithBlock({ (Bool, error) -> Void in
                    })
                 } else {
                    let wanted:PFObject = object as PFObject!
                        if wanted["wanted"] as! Bool == true {
                            if self.newMessages.count > 1 {
                                self.newMessages.removeLast()
                                self.newMessagesNumber.text = "\(self.newMessages.count)"
                            } else {
                                self.newMessages = []
                                self.newMessagesNumber.hidden = true
                            }
                            wanted["wanted"] = false
                            wanted.saveInBackgroundWithBlock{(success, error) -> Void in
                                if success == false {
                                } else {
                                    let currentInstallation:PFInstallation = PFInstallation.currentInstallation()
                                    currentInstallation.badge = self.newMessages.count
                                    currentInstallation.saveEventually()
                                }
                            }
                        }
                }
            }
    }
}

在崩溃之前删除对象,并且似乎重新加载了UITableView。什么可能导致这次崩溃?

1 个答案:

答案 0 :(得分:0)

更新您的代码,以便更好地运作 -

let deleteRow = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

    self.messageTableView.beginUpdates()

    //your code here

    self.messageTableView.endUpdates()
})