2013-04-12 75 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