当您按滑动并按删除时,我正在尝试删除核心数据中的对象。问题是它删除了表格单元格,但当我再次返回时,删除的单元格又回来了。我想那是因为我只删除了NSMUtableArray(设备)中的对象,并且没有删除核心数据对象。我怎么能这样做?
保存对象的saveTap:
-(void)saveTap:(id)sender{
Entity *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:_managedObjectContext];
[newManagedObject setValue:self.textfield.text forKey:@"playlistName"];
[newManagedObject setValue:[NSNumber numberWithInt:selectedRowValue] forKey:@"idnumber"];
// Save the context.
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
[self fetchDevices];
NSLog(@"COUNT %d", [devices count]);
}
和commiteditingstyle方法
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.devices removeObjectAtIndex:indexPath.row];
[tableViewData reloadData]; }
}
答案 0 :(得分:1)
是[self.devices objectAtIndex:indexPath.row];返回要删除的NSManagedObject? 然后你应该做第二个函数:
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_managedObjectContext deleteObject:[self.devices objectAtIndex:indexPath.row]];
[_managedObjectContext save:nil];
[self.devices removeObjectAtIndex:indexPath.row];
[tableViewData reloadData]; }
}
答案 1 :(得分:0)
而不是在UitableView的时候重新加载你应该只重新加载:它在[UITableView beginEditing]之前; 并在重新加载行之后 [UITableView endEditing];