我正在从coredata中检索数据并在tableview中显示它。如果用户点击一行,它将导航到另一个视图(例如视图2)。
现在,我想从tableview和coredata中删除一行。我使用了一个代码,我可以删除该行。但是,当我去观看2并回来时,记录又回到那里。 (它从coredata中删除但是行和它的数据仍然存在,直到我重新加载视图)。
这是我使用的代码。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error;
if (![context save:&error])
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"App" message:@"Sorry the Item Cannot be deleted" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
}
答案 0 :(得分:1)
您是否在删除后尝试reloadData
,
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error;
if (![context save:&error])
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Xpense Tracker" message:@"Sorry the Item Cannot be deleted" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
alertView release];
}
[YOURTABLEVIEW reloadData];
}
已编辑:现在工作正常。试试这种方式
Step 1:
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,NSFetchedResultsControllerDelegate>
Step 2:
update your
-(NSFetchedResultsController *)fetchedResultsController{
self.fetchedResultsController.delegate = self; // with this
}
step 3:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSManagedObjectContext * context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError * error;
if (![context save:&error])
{
NSLog(@"Deletion Error");
}
}
}
Step 4:
include NSFetchedResultsControllerDelegate method
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[YOURTABLEVIEW reloadData];
}
答案 1 :(得分:1)
NSManagedObjectContext
需要保存。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
NSError *error;
[context save:&error] //add this line
if (![context save:&error]) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Xpense Tracker" message:@"Sorry the Item Cannot be deleted" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
}
检查这个..
答案 2 :(得分:0)
根据您的要求更改您的表格和详细信息关系
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObject *selectedOrder = [selectedOrderArray objectAtIndex:indexPath.row];
Order *orderInstance = (Order *) selectedOrder;
numberOfRows = [[orderInstance.detailRelation allObjects]count];
NSString *deletedItem;
deletedItem = (orderInstance.optionName == nil) ? [[NSString alloc]initWithFormat:@"%@ deleted successfully",orderInstance.menuName] : [[NSString alloc]initWithFormat:@"%@ %@ deleted successfully",orderInstance.optionName , orderInstance.menuName];
[self.managedObjectContext deleteObject:selectedOrder];
if([self performSaveInMemoryObjectContext:self.managedObjectContext]){
// [self.tableview reloadData];
[common ShowAlert:deletedItem andMessage:@""];
[selectedOrderArray removeObjectAtIndex:indexPath.row];
}
[self fetchRecords];
// NSLog(@"count... %d",[selectedOrderArray count]);
[tableview reloadData];
}
}