简体   繁体   English

parse.com swift-取消固定并删除tableView单元格中的对象

[英]parse.com swift - unpin and delete object in tableView cell

im making an app that currently saves a parse object to the local datastore, and to parse.com. 我正在制作一个当前将解析对象保存到本地数据存储以及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. 其中“ arrayToPopulateCells”是在我的查询期间存储解析对象的位置。

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? 显然,我无法删除indexpath.row上的单元格,但是如何获取该单元格中的信息,并找到匹配的解析对象以取消固定和删除?

Using this delegate u can get reference of lighthouseCell and using that u can get variable and 使用此委托,您可以获取lighthouseCell的引用,使用该代理,您可以获取变量和

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然后

selectedCell.data.removeObjectForKey("keyName")

and for delete parse data we have Delete Object for particular key 对于删除解析数据,我们具有用于特定键的Delete Object

Assuming the objects in your array are actual PFObject instances, then just call deleteInBackground before you remove them from your data array. 假设数组中的对象是实际的PFObject实例,则只需在将其从数据数组中删除之前调用deleteInBackground

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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM