我有一个名为Word
的核心数据表,其中有5个attribute
。它们是word
,synonym
,definition
,sentence
& date
。我在UITableView
中显示所有数据。但是我的tableView的值index
与代码数据对象index
不同。让我展示一下我的代码:
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];
}
我根据新的tableView
词典在我的self.wordDic
中显示数据。所有数据按字母顺序排列。现在我要删除tableView
中的数据。为此,我做了以下几点。
- (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];
}
}
但它已经崩溃了。原因在于这一行:
[self.wordListArray removeObjectAtIndex:indexPath.row];
wordListArray
保存我的核心数据原始对象,我想删除我的表视图中的索引与该索引不同的值。
我的问题是,正如您所看到的,我在[secData objectAtIndex:indexPath.row]]
处拥有属性值,使用此方法如何在self.wordListArray
对象列表中获取具有原始核心数据的索引值索引?
核心数据中的实际Word
表:
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
在我的tableView
我按照这样排序:
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
现在我要删除,从我的tableView中说出Bubble
行,UITableView
中的当前索引是3
,但在我的实际核心数据对象中,它的索引是6
。如何在核心数据对象中获取我的值Bubble
的原始索引?
如果您了解我的问题,请回复。非常感谢提前。
祝你有个美好的一天。
被修改
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([selectedIndex isEqual:indexPath])
{
// Expanded Cell
return cell;
}
else
{
static NSString *cellIdentifier = kHomeTableViewCellID;
HomeTableViewCell *cell = (HomeTableViewCell *)[self.homeTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *secTitle = [self.keyArray objectAtIndex:indexPath.section];
NSMutableArray *secData = [self.wordDic objectForKey:secTitle];
[secData sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSString *data = [secData objectAtIndex:indexPath.row];
[cell.wordLabel setText:data];
// NSManagedObject *words = [self.wordListArray objectAtIndex:indexPath.row];
// [cell.wordLabel setText:[NSString stringWithFormat:@"%@", [words valueForKey:@"word"]]];
return cell;
}
}
答案 0 :(得分:2)
您没有利用NSFetchedResultsController
的任何特殊原因?这是其用例之一。
答案 1 :(得分:2)
您没有发布tableView:cellForRowAtIndexPath:
,但在该函数中,您已经解决了如何将indexPath转换为wordListArray的索引。
您还需要在动画块中调用“deleteRows”。
所以,替换这段代码:
// remove info from tableView array
[self.wordListArray removeObjectAtIndex:indexPath.row];
[self.homeTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[secData objectAtIndex:indexPath.row]] withRowAnimation:UITableViewRowAnimationFade];
使用:
// remove info from tableView array
NSInteger wordArrayIndex = [self wordArrayIndexForTablePath:indexPath];
[self.wordListArray removeObjectAtIndex:wordArrayIndex];
// batch deletions must be in an animation block
[self.homeTableView beginUpdates];
[self.homeTableView deleteRowsAtIndexPaths:@[indexPath]] withRowAnimation:UITableViewRowAnimationFade];
[self.homeTableView endUpdates];
这是wordArrayIndexForTablePath(你需要填写内容)
- (NSInteger)wordArrayIndexForTablePath:(NSIndexPath)indexPath {
NSString *tableData = /* refactor out the section/row part from cellForIndexPath and look this up with indexPath */;
for (NSInteger i = 0; i < self.wordListArray.count; ++i) {
// I have no idea how all of your arrays and structures map
// to each other. You need to find the object in the array
// that matches your section/row table data
if (/* you need to figure this part out -- use tableData, I guess */) {
return i;
}
}
// you probably want to assert here if you think it's always there
return -1;
}