gpt4 book ai didi

iphone - 从 tableView 和 coredata 中删除一行

转载 作者:行者123 更新时间:2023-11-29 11:01:25 26 4
gpt4 key购买 nike

我正在从 coredata 中检索数据并将其显示在 tableview 中。如果用户点击一行,它将导航到另一个 View (比如 View 2)。

现在,我想从 tableview 和 coredata 中删除一行。我使用了一个代码,可以删除该行。但是,当我去查看 2 并返回时,记录又回到了那里。 (它已从 coredata 中删除,但该行及其数据仍然存在,直到我重新加载 View )。

这是我使用的代码。

- (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];
}
}
}

最佳答案

删除后,您是否尝试过使用 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];
}

关于iphone - 从 tableView 和 coredata 中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15740383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com