我在寫一個簡單的應用程序,允許用戶將條目添加到數據庫中。數據顯示在UITableView中。我無法弄清楚的是,如何使用tableview的輕掃 - 刪除功能從數據庫中刪除一條記錄。我知道代碼放在此方法:使用UITableView和核心數據從數據庫中刪除一條記錄
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
}
但我不知道如何獲取用於填充已刷卡細胞數據庫的記錄。
我當用戶點擊導航欄上的一個按鈕,刪除所有細胞的方法:
-(void)deleteAll {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Parameters" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *items = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *managedObject in items) {
[context deleteObject:managedObject];
}
if (![context save:&error]) {
}
[self.tableView reloadData];
}
但我不如何自定義該代碼同時刪除一條記錄。任何幫助讓我開始將不勝感激。
我有這樣的方法,以及...我認爲這會永久刪除記錄,但它不...
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[self.arr removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
[self.tableView reloadData];
}
我有這樣的方法commitEditingStyle已經獲得刷卡單元格的索引路徑(和參數對象),但我不明白我價值傳遞給NSPredicate的predicateWithFormat方法。原諒我,我是Core Data的全新人物,在整體編碼方面仍然很新穎。 – pjv
您的表視圖必須包含參數類對象的數組。使用-commitEditingStyle方法的索引路徑拾取特定的參數對象。然後傳遞任何實例變量及其值作爲謂詞的參數。如果仍然難以理解,請發佈Parameter對象的頭文件,也許我可以幫助你。 – Mani