【问题标题】:parse.com swift - unpin and delete object in tableView cellparse.com swift - 在 tableView 单元格中取消固定和删除对象
【发布时间】:2015-09-08 03:21:44
【问题描述】:

我正在制作一个应用程序,该应用程序当前将解析对象保存到本地数据存储区和 parse.com。然后我运行一个查询,将每个对象保存到一个数组中,以便我可以在表格视图中显示它。由于我过去发布的问题herehere,到目前为止一切正常。

所以现在信息正按我希望的那样正确显示。但现在,我也希望能够删除条目。我想滑动表格,当点击删除时,对象会从本地数据存储中取消固定,并且相同的项目也会从云中删除。

这是我目前的代码:

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

【问题讨论】:

  • 你能告诉我你在 CellForRowAtINDex TableView Delegate 中写了什么

标签: ios swift uitableview parse-platform


【解决方案1】:

使用这个委托你可以获得 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 for particular key

【讨论】:

    【解决方案2】:

    假设您的数组中的对象是实际的 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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多