我用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