2013-04-12 77 views
0

我在寫一個簡單的應用程序,允許用戶將條目添加到數據庫中。數據顯示在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]; 
} 

回答

0

我也做了同樣的滑動刪除功能使用MCSwipeTableCell

對於從表中刪除一個特定的行動畫做到這一點:

//I am passing 0,0 so you gotta pass the row that was deleted. 
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0] 

[self.yourTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

對於從核心數據刪除這樣做:

YourCustomModel *modelObj; 

NSManagedObjectContext *context= yourmanagedObjectContext; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:[NSEntityDescription entityForName:@"YourCustomModel" inManagedObjectContext:context]]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"yourField == %@", passTheFieldValueOfTheRowDeleted]; 
[request setPredicate:predicate]; 

NSError *error = nil; 
NSArray *results = [context executeFetchRequest:request error:&error]; 

if ([results count]>0) 
{ 
    modelObj = (Customers *)[results objectAtIndex:0]; 
} 

[context deleteObject:modelObj]; 

if (![context save:&error]) 
{ 
    NSLog(@"Sorry, couldn't delete values %@", [error localizedDescription]); 
} 
0

你幾乎就在那裏..只要向提取請求添加一個謂詞即可。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(somePropertyInParameters = %d)",value]; 
    [fetchRequest setPredicate:predicate];` 

另外,還可以在這個方法中

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath` 
+0

我有這樣的方法commitEditingStyle已經獲得刷卡單元格的索引路徑(和參數對象),但我不明白我價值傳遞給NSPredicate的predicateWithFormat方法。原諒我,我是Core Data的全新人物,在整體編碼方面仍然很新穎。 – pjv

+0

您的表視圖必須包含參數類對象的數組。使用-commitEditingStyle方法的索引路徑拾取特定的參數對象。然後傳遞任何實例變量及其值作爲謂詞的參數。如果仍然難以理解,請發佈Parameter對象的頭文件,也許我可以幫助你。 – Mani