簡體   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