1

im making an app that currently saves a parse object to the local datastore, and to parse.com. I then run a query, save each object into an array so that i can display it in a table view. everything up to this point works well thanks to my past posted questions here and here.

so now the info is being displayed correctly as i had hoped. but now, i would like to be able to delete the entries as well. I would like to swipe the table and when delete is tapped, the object is unpinned from the local datastore, and that same item is removed from the cloud as well.

here is my current code for this :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {



    // var lighthouse = self.lighthouses[indexPath.row]
    var data = self.arrayToPopulateCells[indexPath.row]

    let cell = tableView.dequeueReusableCellWithIdentifier("locationCell") as! lighthouseCell

    let row = indexPath.row
    cell.cellName.text = data.name
    cell.cellPlace.text = data.locality

    cell.cellDate.text = "\(data.date)"

    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.selectedLighthouse = self.arrayToPopulateCells[indexPath.row]
    self.performSegueWithIdentifier("lighthouseDetailViewSegue", sender: self)
}



func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {

        self.arrayToPopulateCells.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

where "arrayToPopulateCells" is where the parse objects get stored during my query.

obviously im able to remove the cell at indexpath.row, but how to i take the information in that cell, and find the matching parse object to unpin and delete?

1
  • Can you tell me what you write in CellForRowAtINDex TableView Delegate Commented Sep 8, 2015 at 4:42

2 Answers 2

0

Using this delegate u can get reference of lighthouseCell and using that u can get variable and

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

if (editingStyle == UITableViewCellEditingStyle.Delete) {

     var selectedCell = tableView.cellForRowAtIndexPath(indexPath) as lighthouseCell
        gameScore.removeObjectForKey("playerName")
        self.arrayToPopulateCells.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}

using selectedCell and then

selectedCell.data.removeObjectForKey("keyName")

and for delete parse data we have Delete Object for particular key

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming the objects in your array are actual PFObject instances, then just call deleteInBackground before you remove them from your data array.

So:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if (editingStyle == UITableViewCellEditingStyle.Delete) {
        self.arrayToPopulateCells[indexPath.row].deleteInBackground() // Delete from cloud
        self.arrayToPopulateCells.removeAtIndex(indexPath.row)
        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.