我有一个核心数据表Word
,它有5个attribute
。它们是word
,synonym
,definition
,sentence
& date
。我在UITableView
中显示所有数据。但是我的tableView的值index
与代码数据对象index
不一样。让我告诉我的代码:如何找出核心数据模型中特定属性索引的值iOS
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:'代码编辑了我的帖子。这里我使用了过滤的NSArray。不是核心数据对象数组。 – Tulon
我添加了一些pseduo代码。你需要用你的数据结构知识来填充它。 –
谢谢你的解释,先生。我这样做,它的工作完美。 :) – Tulon