我在写一个简单的应用程序,允许用户将条目添加到数据库中。数据显示在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