如何在核心数据模型iOS中找出特定属性索引的值

Posted

技术标签:

【中文标题】如何在核心数据模型iOS中找出特定属性索引的值【英文标题】:How to find out value of a particular attribute index in core data model iOS 【发布时间】:2016-01-22 13:46:34 【问题描述】:

我有一个名为Word 的核心数据表,其中有5 个attribute。它们是wordsynonymdefinitionsentencedate。我在UITableView 中显示所有数据。但是我的tableView 的值index 与代码数据对象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];
    

我根据新的self.wordDic 字典在我的tableView 中显示数据。所有数据按字母顺序排列。现在我想从我的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

现在我想从我的 tableView 中删除,比如说 Bubble 行,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;
    

【问题讨论】:

【参考方案1】:

你没有发布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; 
   

【讨论】:

感谢您的回复。我用tableView:cellForRowAtIndexPath: 代码编辑了我的帖子。这里我使用了过滤后的NSArray。不是核心数据对象数组。 我添加了一些伪代码。你需要用你对数据结构的了解来填写它。 感谢先生的解释。我这样做了,而且效果很好。 :)【参考方案2】:

您没有使用NSFetchedResultsController 的任何特殊原因?这是它的用例之一。

【讨论】:

感谢您的回复。好吧,不是那样的。这肯定是一个选择。

以上是关于如何在核心数据模型iOS中找出特定属性索引的值的主要内容,如果未能解决你的问题,请参考以下文章

如何查询具有特定属性值的核心数据对象

在核心数据映射模型的值表达式中使用“indexOfObject:”

MySQL索引

如何在 iOS 核心数据中获取更多属性的请求?

什么是核心数据模型中的获取索引元素?

核心数据更新所有“索引”属性(iOS)