2016-01-22 57 views
1

我有一個核心數據表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,其當前索引在UITableView3,但在我的實際核心數據對象中,它的索引是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; 
    } 
} 

回答

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; 
    } 
+0

感謝您的回覆。我用'tableView:cellForRowAtIndexPath:'代碼編輯了我的帖子。這裏我使用了過濾的NSArray。不是核心數據對象數組。 – Tulon

+0

我添加了一些pseduo代碼。你需要用你的數據結構知識來填充它。 –

+1

謝謝你的解釋,先生。我這樣做,它的工作完美。 :) – Tulon