这是我用于表视图控制器的代码。问题是行没有被删除而且没有显示在我的表视图中? 我应该怎么做才能从我的行中的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)
{
}
}
现在如何理清问题?
答案 0 :(得分:1)
基于视图的表视图中的常用方法是从数据库,内容数组和更新块中的表视图中删除对象。
- (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)
{
}
}
答案 1 :(得分:0)
问题是因为您持有fetchedObjects
的强引用,删除后只会重新加载表格视图。
另外,您没有要求managedObjectContext
保留/保存更改
所以你的代码应该更像:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[_managedObjectContext deleteObject:[_studentattributes objectAtIndex:indexPath.row]];
[_managedObjectContext save:nil]; // Tell the context to save the changes.
[self fetchdata]; // Needs to fetch data again so your _studentattributes you have the right count.
[self.tableview reloaddata];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
}
}
答案 2 :(得分:0)
首先,我发现你多次打电话给fetchdata
。我建议您在loadView
中拨打一次,然后在[self.tableview reloadData]
中拨打viewWillAppear
。另请注意,您在表格视图中呼叫reloaddata
而不是reloadData
。
接下来,管理对象上下文是一个便笺簿,您可以在其中进行更改。但最终你需要通过调用来保存它:
if (![context save:&error]) {
NSLog(@"Couldn't save: %@", error);
}
PS:根据您对Adeel的评论,我假设您的数据源_studentattributes
在您的桌面视图上再次调用reloadData
之前已正确更新,因为这会驱动表视图外观和感觉。