Вот мой код, который я использую для контроллера табличного представления. Проблема в том, что строки не удаляются и не отображаются в моем представлении таблицы? Что делать, чтобы удалить мои данные из базы данных 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)
{
}
}
теперь, как разобраться в проблеме?
Удалите объект из массива _studentattributes, а также после удаления записи из coredata. Также перезагрузите таблицу. – Adeel
@AdeelMiraj Его удаление, но когда я запускаю приложение снова, он показывает ту же строку снова –
ссылается на ответ в этой теме http://stackoverflow.com/questions/10482311/delete-an-object-in-core-data – Adeel