Uitableview 核心数据:deleteObject不会触发表更新

Uitableview 核心数据:deleteObject不会触发表更新,uitableview,core-data,nsfetchedresultscontroller,Uitableview,Core Data,Nsfetchedresultscontroller,我有一个带有菜单项的UITableView,当菜单更改时,服务器会将新列表推送到设备上。当我添加菜单项或提交更改时更改菜单项时,显示列表的表视图会自动更新 但是,当我删除一个对象时,这不会反映在列表中(当我更改顺序时,也不会反映,这意味着对象的“级别”字段) 这些更改仅在我完全关闭(杀死)应用程序并重新启动后显示 没有触发更新的原因很简单吗 for(DOMenuItem* item in _oldMenuIdList){ [[self managedObjectContext] dele

我有一个带有菜单项的UITableView,当菜单更改时,服务器会将新列表推送到设备上。当我添加菜单项或提交更改时更改菜单项时,显示列表的表视图会自动更新

但是,当我删除一个对象时,这不会反映在列表中(当我更改顺序时,也不会反映,这意味着对象的“级别”字段)

这些更改仅在我完全关闭(杀死)应用程序并重新启动后显示

没有触发更新的原因很简单吗

for(DOMenuItem* item in _oldMenuIdList){
    [[self managedObjectContext] deleteObject:item]; 
}

NSError *error;

if(![[self managedObjectContext] save:&error]) {
    NSLog(@"Error!");
}
[编辑:]

@interface DOMenuItem : NSManagedObject {

}

@property (nonatomic, strong) NSNumber * id;
@property (nonatomic, strong) NSString * action;
@property (nonatomic, strong) NSString * title;
@property (nonatomic, strong) NSString * section;
@property (nonatomic, strong) NSNumber * level;
@property (nonatomic, strong) NSNumber * published;
@property (nonatomic, strong) NSString * icon;
@property (nonatomic, strong) NSDate * lastUpdate;
@property (nonatomic, strong) DOMenuItem * parent;
@property (nonatomic, strong) DOLang * language;
@property (nonatomic, strong) NSSet* children;
@property (nonatomic, strong) NSSet* groups;

@end

@interface DOMenuItem (CoreDataGeneratedAccessors)
- (void)addChildren:(NSSet *)value;
- (void)removeChildren:(NSSet *)value;

- (void)addGroups:(NSSet *)value;
- (void)removeGroups:(NSSet *)value;
- (void)addGroupsObject:(DOGroup *)value;
- (void)removeGroupsObject:(DOGroup *)value;
@end
-

[编辑2:] 在UIViewController中:

- (void)viewDidLoad
{
iDomsAppDelegate *delegate = (iDomsAppDelegate *)[[UIApplication sharedApplication] delegate];

// Get the menu's
_objects = [[[DOMenuItemsController alloc] init:[delegate managedObjectContext]] findChildrenOfMenu:(DOMenuItem*) _parentMenu];

[_objects setDelegate:self];
}
[编辑3:]

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)aSectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    NSUInteger sectionIndex = aSectionIndex;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeMove:
            NSLog(@"NEED TO LOOK AT THIS!");
            break;
        case NSFetchedResultsChangeUpdate:
            NSLog(@"NEED TO LOOK AT THIS!");
            break;

    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)aIndexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)aNewIndexPath {

    UITableView *tableView = [self tableView];

    switch(type) {
        case NSFetchedResultsChangeInsert: {
            //NSLog(@"Inserted!");
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:aNewIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            }
            break;

        case NSFetchedResultsChangeDelete:{
            //NSLog(@"Delete");

            // Skip if previous in section had same commonId
            NSLog(@"Table delete... r:%li - s:%li", (long)aIndexPath.row, (long)aIndexPath.section);

            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            }
            break;

        case NSFetchedResultsChangeUpdate:
            //NSLog(@"Update menu");
            [(DOPrototypeCell*) [tableView cellForRowAtIndexPath:aIndexPath] populateCell:[_objects objectAtIndexPath:aIndexPath]];
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeMove:{
            //NSLog(@"Move");
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];

            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:aNewIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            }
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    @try {
        [self.tableView endUpdates];
    } @catch (NSException *exception) {
        NSLog(@"Caught error on finding tabelview update: %@", exception.description);
        [self.tableView reloadData];
    }

    [self checkViewheight];
}
-(void)controllerWillChangeContent:(NSFetchedResultsController*)控制器{
[self.tableView开始更新];
}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeSection:(id)sectionInfo
atIndex:(nsInteger)changeType:(NSFetchedResultsChangeType)类型的操作索引{
NSU整数sectionIndex=ASEOCTIONINDEX;
开关(类型){
案例NSFetchedResultsChangesInsert:
[self.tableView insertSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeMove:
NSLog(@“需要看看这个!”);
打破
案例NSFetchedResultsChangeUpdate:
NSLog(@“需要看看这个!”);
打破
}
}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeObject:(id)一个对象
atIndexPath:(NSIndexPath*)更改类型的ADINDEXPATH:(NSFETCHEDEDRESULTSCHANGETPE)类型
newindepath:(nsindepath*)anewindepath{
UITableView*tableView=[self tableView];
开关(类型){
案例NSFetchedResultsChangesInsert:{
//NSLog(@“已插入!”);
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:aNewIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
打破
案例NSFetchedResultsChangeDelete:{
//NSLog(@“删除”);
//如果节中的上一个具有相同的commonId,则跳过
NSLog(@“表删除…r:%li-s:%li”,(长)aIndexPath.row,(长)aIndexPath.section);
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
打破
案例NSFetchedResultsChangeUpdate:
//NSLog(“更新菜单”);
[(DOPrototypeCell*)[tableView cellForRowAtIndexPath:aIndexPath]populateCell:[\u objects objectAtIndexPath:aIndexPath]];
[tableView重新加载RowsAndExpaths:[NSArray arrayWithObject:aIndexPath]和RowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeMove:{
//NSLog(@“移动”);
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:aNewIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
打破
}
}
-(void)controllerDidChangeContent:(NSFetchedResultsController*)控制器{
@试一试{
[self.tableView endUpdates];
}@catch(NSException*exception){
NSLog(@“在查找选项卡视图更新时捕获错误:%@”,异常。说明);
[self.tableView重载数据];
}
[自检视图高度];
}

请发布表视图数据源方法,如果您使用的是NSFetchedResultsController,请发布FRC初始化和委托方法。谢谢。添加的信息有用吗?是的,谢谢。您是否在任何地方设置FRC
代表
?我在那些方法中看不到它。是的,在UIViewController中,在viewDidLoad上(我添加了代码,但它不是什么特别的东西)。“objects”是从前面引用的过程返回的NSFetchedResultsController。我不明白的是,为什么新行和内容更新有效,但删除(以及顺序的更改,如果支持的话)无效。是的-这表明
controller:didChangeObject:atIndexPath:forChangeType:…
方法有问题。其中通常有一个
switch
语句,其中包含不同的
大小写
,用于不同的更改类型:插入、删除、移动和更新。你有那样的东西吗?
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)aSectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    NSUInteger sectionIndex = aSectionIndex;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                          withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeMove:
            NSLog(@"NEED TO LOOK AT THIS!");
            break;
        case NSFetchedResultsChangeUpdate:
            NSLog(@"NEED TO LOOK AT THIS!");
            break;

    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)aIndexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)aNewIndexPath {

    UITableView *tableView = [self tableView];

    switch(type) {
        case NSFetchedResultsChangeInsert: {
            //NSLog(@"Inserted!");
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:aNewIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            }
            break;

        case NSFetchedResultsChangeDelete:{
            //NSLog(@"Delete");

            // Skip if previous in section had same commonId
            NSLog(@"Table delete... r:%li - s:%li", (long)aIndexPath.row, (long)aIndexPath.section);

            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            }
            break;

        case NSFetchedResultsChangeUpdate:
            //NSLog(@"Update menu");
            [(DOPrototypeCell*) [tableView cellForRowAtIndexPath:aIndexPath] populateCell:[_objects objectAtIndexPath:aIndexPath]];
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeMove:{
            //NSLog(@"Move");
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:aIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];

            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:aNewIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            }
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    @try {
        [self.tableView endUpdates];
    } @catch (NSException *exception) {
        NSLog(@"Caught error on finding tabelview update: %@", exception.description);
        [self.tableView reloadData];
    }

    [self checkViewheight];
}