2012-12-11 32 views
0

我用2个单位的人与事件,通过关系连接的“事件”一对多如何更新的tableView。在我的第一个表格视图中,我有Person的名字,我有他们的Events类型。我可以向特定人员插入新事件,但第二个表格视图不会更新的问题。只有当我使用导航仪回到人员,并且在它再次发生事件后,我才能看到变化。当插入新的数据关系

用于插入新的事件我用这个方法:

- (void) addEventControllerDidSave:(NSString *)typeData{ 

    NSManagedObjectContext* context = [self managedObjectContext]; 
    Event *newEvent = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:context]; 
    [newEvent setType:typeData]; 
    [currentPerson addEventsObject:newEvent]; 

    NSError *error; 
    if (![[self managedObjectContext] save:&error]) 
    { 
     NSLog(@"Problem saving: %@", [error localizedDescription]); 
    } 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

我如何定义单元格:

//create cell use relation 
    NSArray *eventsArray = [[[[self currentPerson] events] objectEnumerator]allObjects]; 
    Event *newEvent = [eventsArray objectAtIndex:indexPath.row]; 
    [[cell textLabel]setText: [newEvent type]]; 

更新表法:

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

- (void) controllerDidChangeContent:(NSFetchedResultsController *)controller{ 
    [[self tableView] endUpdates]; 
} 

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

    UITableView *tableView = [self tableView]; 
    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete: 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeUpdate: 
     { 
      NSArray *eventsArray = [[[[self currentPerson] events] objectEnumerator]allObjects]; 
      Event *newEvent = [eventsArray objectAtIndex:indexPath.row]; 
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
      [[cell textLabel]setText: [newEvent type]]; 
      break; 
     } 
     case NSFetchedResultsChangeMove: 
     { 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     } 
     default: 
      break; 
    }//switch 
} 

- (void) controller: (NSFetchedResultsController *) controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 
{ 
    switch (type) { 
     case NSFetchedResultsChangeInsert: 
      [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     case NSFetchedResultsChangeDelete: 
      [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] 
          withRowAnimation:UITableViewRowAnimationFade]; 
      break; 
     default: 
      break; 
    } 
} 

我真的不知道如何更新tableView正确,我更新我的第一个tableView,但第二个有问题,我描述早(也许我计算负担,因为我用的关系) 你还可以看到我的完整的项目到现在为止,在git的(也许它可以帮助):

https://github.com/dennis87/eventCost

回答

1

EventsTableControllerfetchResultsController性质和相应的_fetchResultsController实例变量。但是这个FRC始终是nil,因为它永远不会在EventsTableController中分配+初始化。

从不会调用更新方法controllerWillChangeContent,didChangeObject,controllerDidChangeContent,因为您没有带委托的FRC。

作为一种变通方法,您可以在addEventControllerDidSave添加

[self.tableView reloadData]; 

,然后新的项目将立即可见。

但是真正的解决方案是初始化和使用FRC,就像您在NamesTableController中已经完成的那样。然后新的事件会自动显示。


创建获取结果控制器,获取当前人的所有事件是这样的(这是从NamesTableController你的代码,修改后的使用中EventsTableController):

-(NSFetchedResultsController *) fetchResultsController{ 
    if (_fetchResultsController != nil) { 
     return _fetchResultsController; 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" 
               inManagedObjectContext:[self managedObjectContext]]; 
    [fetchRequest setEntity:entity]; 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"person = %@", self.currentPerson]; 
    [fetchRequest setPredicate:predicate]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"type" ascending:YES]; 
    NSArray *sortArray = [[NSArray alloc]initWithObjects:sort, nil]; 
    [fetchRequest setSortDescriptors:sortArray]; 

    _fetchResultsController = [[NSFetchedResultsController alloc] 
           initWithFetchRequest:fetchRequest 
           managedObjectContext:[self managedObjectContext] 
           sectionNameKeyPath:nil 
           cacheName:nil]; 

    _fetchResultsController.delegate = self; 
    return _fetchResultsController; 
} 
+0

再次感谢你!,我把这行合成fetchResultsController = _eventsFetchResultsController; 和方法喜欢我的名字TableController当然我改变_NFC为_eventsNFC ..实体和属性 但我仍然有这个问题,我做错了什么? – Dennis

+0

@Dennis:NamesTableController有一个方法' - (NSFetchedResultsController *)fetchResultsController',其中FRC是使用获取请求和排序描述符创建的。 - 您必须为EventsTableController添加一个类似的方法,在这里为事件创建一个FRC。 –

+0

这正是我所做的,你可以看到我在这里做的新提交:https://github.com/dennis87/eventCost/commit/7614a25258f08920dbe69ff7a7eab1785a0143f6(评论对象) 但在某些原因,这不起作用。 – Dennis

相关问题