2016-04-25 38 views
0

基本上刪除單元格「脫機」我使用這種方法,以便每當你從右向左滑動用戶可以刪除tableview單元格。如何刪除indexPath處的CKRecord? (UITableView)

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if(editingStyle == UITableViewCellEditingStyle.Delete){ 
     self.polls.removeAtIndex(indexPath.row) 
} 

但是,它顯然不會影響我之前創建的單元格內容的CKRecord。那麼我怎樣才能在用戶滑動刪除的確切行上獲取和刪除CKRecord數據呢?

+1

這取決於你的模型。什麼是「民意調查」? – vadian

+0

民意調查是CKRecord – user3545063

回答

1

假設polls是聲明爲[CKRecord]的數據源數組,您必須做三件事。

  1. 從給定索引處的數據源數組中獲取記錄,並將其從適當的CKDatabase中刪除。
  2. 從數據源數組中刪除記錄(您已經這樣做了)。
  3. 刪除表格視圖中的行deleteRowsAtIndexPaths傳遞[indexPath]

例如(publicDatabase是實際CKDatabase實例):

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     let record = polls[indexPath.row] 
     publicDatabase.deleteRecordWithID(record.recordID, completionHandler: ({returnRecord, error in 
      // do error handling 
     }) 
     polls.removeAtIndex(indexPath.row) 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } 
} 

編輯:

對於正確的錯誤處理,你可能要忍受步驟二和三的代碼到完成塊。

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
     let record = polls[indexPath.row] 
     publicDatabase.deleteRecordWithID(record.recordID, completionHandler: ({returnRecord, error in 
      if error != nil { 
      // do error handling 
      } else { 
      self.polls.removeAtIndex(indexPath.row) 
      dispatch_async(dispatch_get_main_queue()) { 
       self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
      } 
      } 
     }) 
    } 
} 
+0

感謝您的回答。是的,民意調查是數據源數組(我不知道如何調用它) 問題是我真的不知道如何從給定索引處的民意調查中獲得記錄。你能詳細說明一下嗎? – user3545063

+0

我編輯了答案。 – vadian

+0

感謝您抽出時間!我剛開始使用「嚴肅」的iOS開發,並且查看代碼示例對我來說理解這個概念最合適。 – user3545063

相關問題