Mike
Mike

Reputation: 85

Cells are only deleted/added once leaving and returning

I don't think it's necessary to post my code for this issue. I am just looking for an explanation or probably one line of code to solve my problem. When I delete or add a cellForRowAtIndexPath within my app, it is both added or deleted to Parse, but does not appear in the app until segueing to a different screen and returning. I have tried moving my delete and add codes into ViewDidLoad and ViewWillAppear

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        var object: PFObject = self.items[indexPath.row] as PFObject
        let alert : UIAlertView = UIAlertView(title: "Item deleted from cart!", message: "", delegate: self, cancelButtonTitle: "Dismiss")
        alert.show()

        object.deleteInBackgroundWithBlock({ (succeed, error) -> Void in

            self.tableView.reloadData()

        })
    }
}

Upvotes: 0

Views: 44

Answers (4)

Ruchish Shah
Ruchish Shah

Reputation: 319

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        var object: PFObject = self.items[indexPath.row] as PFObject
        let alert : UIAlertView = UIAlertView(title: "Item deleted from cart!", message: "", delegate: self, cancelButtonTitle: "Dismiss")
        alert.show()

        object.deleteInBackgroundWithBlock({ (succeed, error) -> Void in

        self.items.removeAtIndex(indexPath.row)
        self.tableView.reloadData()

        })
    }
}

Upvotes: 0

Mike
Mike

Reputation: 85

        items.removeAtIndex(indexPath.row)

Had to add this line above the object.deleteInBackgroundWithBlock

Upvotes: 0

Frankie
Frankie

Reputation: 11928

You are deleting the item from Parse, but you are not deleting the item from your self.items array, which is probably the data source to your table view. The self.items array is probably initialized in your viewDidLoad or viewWillAppear method from Parse, which is why you're seeing it update when you come back to the screen. Just a guess.

Upvotes: 1

Jonathan Francis
Jonathan Francis

Reputation: 174

try tableView.reloadData()

You need to call that method for iOS to look again at your data source and render the tableView.

Upvotes: 0

Related Questions