I have a core data table called Word, which has 5 attribute. They are word, synonym, definition, sentence & date. I am showing all data in UITableView. But the value index of my tableView is not same as code data object index. Let me show my code:
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Words"];
self.wordListArray = [[NSMutableArray alloc] init];
self.wordListArray = [[moc executeFetchRequest:fetchRequest error:nil] mutableCopy];
self.wordInWordListArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.wordListArray count]; i++)
{
self.words = [self.wordListArray objectAtIndex:i];
[self.wordInWordListArray addObject:self.words.word];
}
if ([self.wordListArray count] != 0)
{
self.wordDic = [self sortedDictionary:self.wordInWordListArray];
}
I show data in my tableView based on the new self.wordDic dictionary. Where all data in arranged by alphabetically. Now I want to delete data from my tableView. For that I do the following.
- (void)tableView:(UITableView *) tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *secTitle = [self.keyArray objectAtIndex:indexPath.section];
NSMutableArray *secData = [self.wordDic objectForKey:secTitle];
NSString *str = [secData objectAtIndex:indexPath.row];
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Words" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"word like %@", [secData objectAtIndex:indexPath.row]];
[request setPredicate:predicate];
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
for (NSManagedObject *obj in matchingData)
{
[context deleteObject:obj];
}
[context save:&error];
// remove info from tableView array
[self.wordListArray removeObjectAtIndex:indexPath.row];
[self.homeTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[secData objectAtIndex:indexPath.row]] withRowAnimation:UITableViewRowAnimationFade];
}
}
But it is got crashed. The reason is for this line:
[self.wordListArray removeObjectAtIndex:indexPath.row];
wordListArray hold my core data original objects and I want to delete a value in my table view whose index is different from this index.
My question is, as you can see I have the attribute value here [secData objectAtIndex:indexPath.row]], using this how can I get it's index value in self.wordListArray object list which has the original core data index?
actual Word table in core data:
word synonym definition sentence date
-----------------------------------------------------
Exact xxx xxx xxx xxx
Aim xxx xxx xxx xxx
Brave xxx xxx xxx xxx
Work xxx xxx xxx xxx
Arise xxx xxx xxx xxx
Wise xxx xxx xxx xxx
Bubble xxx xxx xxx xxx
Zoom xxx xxx xxx xxx
In my tableView I sort them like this:
word synonym definition sentence date
-----------------------------------------------------
Aim xxx xxx xxx xxx
Arise xxx xxx xxx xxx
Brave xxx xxx xxx xxx
Bubble xxx xxx xxx xxx
Exact xxx xxx xxx xxx
Wise xxx xxx xxx xxx
Work xxx xxx xxx xxx
Zoom xxx xxx xxx xxx
Now I want to delete, say Bubble row from my tableView, which current index in UITableView is 3, but in my actual core data object it's index is 6. How can I get the original index of my value Bubble in core data object?
If you understand my problem please reply. Thanks a lot in advance.
Have a good day.
Aucun commentaire:
Enregistrer un commentaire