我用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
再次感謝你!,我把這行合成fetchResultsController = _eventsFetchResultsController; 和方法喜歡我的名字TableController當然我改變_NFC爲_eventsNFC ..實體和屬性 但我仍然有這個問題,我做錯了什麼? – Dennis
@Dennis:NamesTableController有一個方法' - (NSFetchedResultsController *)fetchResultsController',其中FRC是使用獲取請求和排序描述符創建的。 - 您必須爲EventsTableController添加一個類似的方法,在這裏爲事件創建一個FRC。 –
這正是我所做的,你可以看到我在這裏做的新提交:https://github.com/dennis87/eventCost/commit/7614a25258f08920dbe69ff7a7eab1785a0143f6(評論對象) 但在某些原因,這不起作用。 – Dennis