繁体   English   中英

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

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

我正在制作一个当前将解析对象保存到本地数据存储以及parse.com的应用。 然后,我运行查询,将每个对象保存到数组中,以便可以在表视图中显示它。 由于我过去在这里这里发表的问题,到目前为止一切都很好。

所以现在信息可以按照我希望的那样正确显示。 但现在,我也希望能够删除这些条目。 我想轻扫表格,然后点击“删除”,将对象从本地数据存储中取消固定,并将同一项目也从云中删除。

这是我目前的代码:

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

其中“ arrayToPopulateCells”是在我的查询期间存储解析对象的位置。

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

使用此委托,您可以获取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)
}
}

使用selectedCell然后

selectedCell.data.removeObjectForKey("keyName")

对于删除解析数据,我们具有用于特定键的Delete Object

假设数组中的对象是实际的PFObject实例,则只需在将其从数据数组中删除之前调用deleteInBackground

所以:

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