【问题标题】:how delete a data in core data with tableview如何使用tableview删除核心数据中的数据
【发布时间】:2014-03-12 15:00:46
【问题描述】:

我找不到如何从我的表视图中删除核心数据实体实例。

这就是我使用来自核心数据的信息更新表格视图的方式:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    Datos * record = [self.fetchedRecordsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ ",record.punto];

    UIImage* image = [UIImage imageWithData:record.imagen];
    cell.imageView.image = image;

    return cell; 
}

我正在尝试使用以下函数来删除实体实例,但它不起作用:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{  
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

【问题讨论】:

  • 为什么不使用 NSFetchedResultsController??

标签: ios objective-c uitableview core-data


【解决方案1】:

好吧,我不确定你从 CoreData 中获取数据的位置,但你需要调用那种 sn-p,删除行时它不会自行删除

- (void) deleteElement:(NSString*)elementId{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"YourTable" inManagedObjectContext:self.managedObjectContext]];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"id=%@",elementId]];

    NSError *error=nil;
    NSManagedObject *fetchedElement = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] lastObject];

    [self.managedObjectContext deleteObject:fetchedElement];

    if (![self.managedObjectContext save:&error]) {
        NSLog(@"problem deleting item in coredata : %@",[error localizedDescription]);
    }
}

你的代码应该是这样的:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

     [self deleteElement:idOfElementToDelete];
}

这是错误:

'无效更新:第0节中的行数无效。更新(2)后现有节中包含的行数必须等于更新之前该节中包含的行数(2),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数(0 移入,0 移出)。'

【讨论】:

  • 你不应该在答案中添加另一个问题,它不会帮助有同样问题的人,而且可能会造成混淆。最好添加评论。你的新错误与CoreData无关,它说如果你删除一行,新的总数应该与 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  • 对不起,你说得对,错误不是消除核心数据中的数据,tableview中没有清除错误,我用的是[tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic],但不起作用
  • 你可以检查那个链接stackoverflow.com/questions/5043729/… 它是关于插入一个新行但它是同样的问题。
【解决方案2】:

您需要从托管对象上下文中删除实体实例:

NSManagedObjectContext *moc = record.managedObjectContext;
Datos * record = [self.fetchedRecordsArray objectAtIndex:indexPath.row];
[self.fetchedRecordsArray removeObject:record];
[moc deleteObject:record];

NSError *error;

if (![moc save:&error]) {
    NSLog(@"Save error:: %@", error);
}

这是tableView:commitEditingStyle:forRowAtIndexPath:

(假设Datos 是您的托管对象子类)

【讨论】:

  • 好的,是的,但错误是我更新我的问题的tableview行的索引
  • 忘记保存了。调试,对象是否被删除?保存错误是什么?
  • 报错不是消除核心数据中的数据,tableview中没有清除错误,我用的是[tableView deleteRowsAtIndexPaths: [NSArray arrayWithObject: indexPath] withRowAnimation: UITableViewRowAnimationAutomatic],但不起作用
  • 错误是您用来填充表格的任何内容(提供行数)都没有删除项目。我的答案现在是从您的数组和核心数据中删除一个项目。那应该涵盖基础。你用什么返回行数?
  • 返回 [self.fetchedRecordsArray 数量];
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多