2012-12-11 33 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

相關問題