gpt4 book ai didi

ios - 从 UIViewController 中的 UITableView 删除核心数据

转载 作者:行者123 更新时间:2023-11-28 22:58:25 25 4
gpt4 key购买 nike

我在 UIViewController 中有一个 UITableView,这样我就可以在表格下方有一个 UILabel。这样做时,我在添加编辑/完成按钮时遇到了困难。我无法采用传统方式,所以我不得不使用以下想法解决问题:

1) 在 vi​​ewdidload 的左上角创建编辑按钮:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editbutton)];

2) 创建代码,以便在单击编辑按钮时,tableview 变为可编辑,并将标题更改为 done。然后点击完成后,它会返回编辑。

-(IBAction)editbutton{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(donebutton)];
[tableView setEditing:YES animated:YES];

}

-(IBAction)donebutton{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editbutton)];

[tableView setEditing:NO animated:YES];

}

这部分就OK了。为了完整起见,我只是把它放进去了。单击编辑按钮时表格 View 变为可编辑,我可以单击完成并恢复正常。我的问题是单击删除按钮(单击某行旁边的红色减号按钮后)不会删除该行。我尝试了以下代码:

注意:1)上下文已在 .h 文件中声明为:@property (nonatomic, retain) NSManagedObjectContext *context;并合成在.m文件中。2)我在.h文件中声明了@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController 然后在.m文件中声明了@synthesize fetchedResultsController = _fetchedResultsController

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

// Delete the managed object for the given index path
[context deleteObject:[_fetchedResultsController objectAtIndexPath:indexPath]];

// Save the context.
NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.

abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}

编辑:

好的,我找到了一些解决方案。我使用以下代码删除了我的核心数据:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object for the given index path
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
[context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

// Save the context.
NSError *error = nil;
if (![context save:&error]) {
/*
Replace this implementation with code to handle the error appropriately.

abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self fetchedresults];
[self.tableView reloadData];

}
}

我的新问题是当我点击删除时。该行已删除,但仍保留在那里,空白行上有红色减号按钮。我仍然可以点击该行(通常会编辑数据),但没有要加载的数据。

编辑 2:

我忘了添加,为了让它工作,我添加了这个:

- (NSFetchedResultsController *)fetchedResultsController{

if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}

// Set up the fetched results controller.
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Record" inManagedObjectContext:context];
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"activity" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {


NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

return _fetchedResultsController;
}

编辑 3:

这是获取结果的作用:

- (void)fetchedresults {

NSManagedObjectContext *moc = [self context];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Record" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];


NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"activity" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
{
// Deal with error...
}

float tot = [[array valueForKeyPath:@"@sum.cpdhours"] floatValue];
totalhours.text = [NSString stringWithFormat:@"%.1f", tot];


}

最佳答案

我认为您拥有的 managedObjectContext 属性实际上是您的 fetchedResultsController 上下文的父上下文,因此当您删除使用它的实体时,fetchedResultsController 不会知道它应该重新获取和更新 TableView 。尝试调用 [self.fetchedResultsController.managedObjectContext deleteItem:yourItem]。

也许这与我在 iPhone 上写这篇文章并不完全一样,但你明白了。您是否还确保实现获取的结果 Controller 委托(delegate)方法来更新您的 TableView ?

关于ios - 从 UIViewController 中的 UITableView 删除核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10372254/

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