假設polls
是聲明爲[CKRecord]
的數據源數組,您必須做三件事。
- 從給定索引處的數據源數組中獲取記錄,並將其從適當的
CKDatabase
中刪除。
- 從數據源數組中刪除記錄(您已經這樣做了)。
- 刪除表格視圖中的行
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)
}
}
})
}
}
這取決於你的模型。什麼是「民意調查」? – vadian
民意調查是CKRecord – user3545063