我有一个名为Word
5的核心数据表attribute
。它们是word
,synonym
,definition
,sentence
和date
。我正在显示所有数据UITableView
。但是index
tableView的值与代码数据对象不同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
现在,我想Bubble
从tableView中删除行,说当前索引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;
}
}
您没有发布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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句