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

标签 ios objective-c sqlite core-data

我有一个名为 Word 的核心数据表,它有 5 个属性。它们是单词同义词定义句子日期。我在 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 保存我的核心数据原始对象,我想删除 TableView 中索引与此索引不同的值。

我的问题是,正如你所看到的,我这里有属性值[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;
    }
}

最佳答案

您没有发布 tableView:cellForRowAtIndexPath:,但在该函数中,您已经解决了如何将 indexPath 转换为 wordListArray 的索引。

您还需要在动画 block 内调用“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; 
   }

关于ios - 如何找出核心数据模型iOS中特定属性索引的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34948199/

相关文章:

ios - 在 Swift 中设置导航栏高度

ios - 我什么时候应该将 Runloop 应用到我的程序中,为什么?

SQLite Select 查询性能调优

android.database.sqlite.SQLiteException : no such table

sqlite - 在 phonegap 应用程序中导出 sqlite 数据库

iphone - 为 UISplitViewController 的 MasterView 添加 subview

ios - WKWebView媒体控件标题显示Url

iphone - 禁用快速查看

ios - 在 ios 中获取有关音量按钮按下事件的通知

ios - 在多次删除行时删除 UITableView 上的行中的错误