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

我来

我有一个名为Word5的核心数据表attribute它们是wordsynonymdefinitionsentencedate我正在显示所有数据UITableView但是indextableView的值与代码数据对象不同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];
    }

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;
    }
}
卢佛朗哥

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

数据如何存储在核心数据模型中(属性)

来自分类Dev

数据如何存储在核心数据模型中(属性)

来自分类Dev

如何为使用数据模型创建的核心数据类添加属性?

来自分类Dev

考核iOS应用的核心数据模型

来自分类Dev

核心数据数据模型中的订单实体

来自分类Dev

如何在复杂的核心数据模型中有效访问数据

来自分类Dev

尝试为iOS中的音频播放应用程序设计核心数据模型

来自分类Dev

核心数据模型-关系

来自分类Dev

swiftUI核心数据模型

来自分类Dev

核心数据模型规划

来自分类Dev

Xcode核心数据模型缺失

来自分类Dev

如何通过索引属性从核心数据中获取对象?

来自分类Dev

通过核心数据在特定属性中插入值数组

来自分类Dev

ios核心数据获取特定值

来自分类Dev

如何在iOS的核心数据中存储JSON数据

来自分类Dev

如何打开保存在我的笔记本中的数据(使用核心数据模型保存)

来自分类Dev

如何在余烬数据模型中设置关系的值

来自分类Dev

如何在核心数据中存储UIButton值?

来自分类Dev

将iOS应用程序过渡到全新的核心数据数据模型

来自分类Dev

尝试访问核心数据模型的 NSNumber 属性给出 EXC_BAD_ACCESS

来自分类Dev

无需迁移的核心数据更改数据模型

来自分类Dev

重置核心数据模型中的所有已保存数据

来自分类Dev

应用程序的旧版本如何应对较新的核心数据模型?

来自分类Dev

基于图像的核心数据模型关系是否正确?

来自分类Dev

无法识别Xcode 11.4.1核心数据模型

来自分类Dev

基于图像的核心数据模型关系是否正确?

来自分类Dev

核心数据模型似乎未反映在应用程序中

来自分类Dev

Swift:在核心数据模型中搜索字符串

来自分类Dev

iOS 中的核心数据,索引超出范围

Related 相关文章

  1. 1

    数据如何存储在核心数据模型中(属性)

  2. 2

    数据如何存储在核心数据模型中(属性)

  3. 3

    如何为使用数据模型创建的核心数据类添加属性?

  4. 4

    考核iOS应用的核心数据模型

  5. 5

    核心数据数据模型中的订单实体

  6. 6

    如何在复杂的核心数据模型中有效访问数据

  7. 7

    尝试为iOS中的音频播放应用程序设计核心数据模型

  8. 8

    核心数据模型-关系

  9. 9

    swiftUI核心数据模型

  10. 10

    核心数据模型规划

  11. 11

    Xcode核心数据模型缺失

  12. 12

    如何通过索引属性从核心数据中获取对象?

  13. 13

    通过核心数据在特定属性中插入值数组

  14. 14

    ios核心数据获取特定值

  15. 15

    如何在iOS的核心数据中存储JSON数据

  16. 16

    如何打开保存在我的笔记本中的数据(使用核心数据模型保存)

  17. 17

    如何在余烬数据模型中设置关系的值

  18. 18

    如何在核心数据中存储UIButton值?

  19. 19

    将iOS应用程序过渡到全新的核心数据数据模型

  20. 20

    尝试访问核心数据模型的 NSNumber 属性给出 EXC_BAD_ACCESS

  21. 21

    无需迁移的核心数据更改数据模型

  22. 22

    重置核心数据模型中的所有已保存数据

  23. 23

    应用程序的旧版本如何应对较新的核心数据模型?

  24. 24

    基于图像的核心数据模型关系是否正确?

  25. 25

    无法识别Xcode 11.4.1核心数据模型

  26. 26

    基于图像的核心数据模型关系是否正确?

  27. 27

    核心数据模型似乎未反映在应用程序中

  28. 28

    Swift:在核心数据模型中搜索字符串

  29. 29

    iOS 中的核心数据,索引超出范围

热门标签

归档