2016-01-22 58 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