我有一個核心數據表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