gpt4 book ai didi

ios - 如何删除核心数据数据库中的数据?

转载 作者:行者123 更新时间:2023-11-29 12:11:37 27 4
gpt4 key购买 nike

这是我用于 TableView Controller 的代码。问题是行没有被删除并且没有显示在我的 TableView 中?我应该怎么做才能从 coredata 数据库以及我的行中删除我的数据?

我如何使用下面的代码执行此操作:

- (void)viewDidLoad {
[super viewDidLoad];
[self fetchdata];
}
-(void)viewDidAppear:(BOOL)animated
{
[self fetchdata];
[self.tableView reloadData];
}
-(void)fetchdata
{
self.fetchrequest=[[NSFetchRequest alloc]init];
NSEntityDescription *entity=[NSEntityDescription entityForName:@"Student" inManagedObjectContext:_managedObjectContext];
[_fetchrequest setEntity:entity];
NSError *error;
self.fetchedobjects=[_managedObjectContext executeFetchRequest:_fetchrequest error:&error];
if (_fetchedobjects != nil)
{
_studentattributes=[[NSMutableArray alloc]initWithArray:_fetchedobjects];
}
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return _studentattributes.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellreuse" forIndexPath:indexPath];

_STUDENT=[_studentattributes objectAtIndex: indexPath.row];
cell.textLabel.text=_STUDENT.name;

_namearray=[[NSMutableArray alloc]init];
[self.namearray addObject:_STUDENT.name];
_classarray=[[NSMutableArray alloc]init];
[self.classarray addObject:_STUDENT.standard];
_sectionarray=[[NSMutableArray alloc]init];
[self.sectionarray addObject:_STUDENT.section];
_rollarray=[[NSMutableArray alloc]init];
[self.rollarray addObject:_STUDENT.roll];

return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"seguereuse"])
{
EnterDetailViewController *enterdetail=(EnterDetailViewController *)[segue destinationViewController];
enterdetail.managedObjectContext=self.managedObjectContext;
}

else if ([segue.identifier isEqualToString:@"seguedisplay"])
{
NSIndexPath *indexpath=[self.tableView indexPathForSelectedRow];
DisplayViewController *display=(DisplayViewController *)[segue destinationViewController];
display.studentdetails=[[NSMutableArray alloc]initWithObjects:[_namearray objectAtIndex:indexpath.row],[_classarray objectAtIndex:indexpath.row],[_sectionarray objectAtIndex:indexpath.row],[_rollarray objectAtIndex:indexpath.row], nil];
}
}



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

[_managedObjectContext deleteObject:[_studentattributes objectAtIndex:indexPath.row]];


[self.tableview reloaddata];

}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{

}

}

现在如何解决这个问题?

最佳答案

在基于 View 的 TableView 中,通常的方法是从数据库、内容数组和更新 block 中的 TableView 中删除对象。

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[tableView beginUpdates];
NSManagedObject *objectToDelete = [_studentattributes objectAtIndex:indexPath.row];
[_managedObjectContext deleteObject: objectToDelete];
[_studentattributes removeObject: objectToDelete];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
NSError *error;
[_managedObjectContext save:&error];
if (error) {
NSLog(@"An error occurred while deleting a row: %@", error);
}
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{

}
}

关于ios - 如何删除核心数据数据库中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33301135/

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