2015-09-08 23 views
1

即時製作一個當前將解析對象保存到本地數據存儲區和parse.com的應用程序。然後我運行一個查詢,將每個對象保存到一個數組中,以便我可以在一個表視圖中顯示它。到目前爲止,一切都很好,謝謝我過去發佈的問題herehereparse.com swift - 取消固定和刪除tableView單元格中的對象

所以現在信息顯示正確,因爲我希望。但現在,我希望能夠刪除條目。我想滑動表格,並在點擊刪除時,該對象從本地數據存儲中取消固定,並且該相同的項目也從雲中移除。

這裏是我當前的代碼如下:

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處的單元格,但是如何獲取該單元格中的信息並找到匹配的解析對象以取消固定和刪除?

+0

你能告訴我你在CellForRowAtINDex TableView中代表寫的是什麼 – Kavita

回答

0

使用該委託u能得到lighthouseCell的參考和使用u可以使用selectedCell得到變量和

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.data.removeObjectForKey("keyName") 

和刪除解析數據,我們有Delete Object for particular key

0

假設陣列中的對象是實際的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) 
    } 
} 
相關問題