塞格不推动新的房地产数据直到第二次推动?

时间:2013-12-31 03:07:37

标签: ios iphone tableview relationships

所以我正在努力学习更多关于核心数据的知识,并且我正努力使用子类来处理关系。我希望能够使用以下类型的视图方案最终做一个稍微高级的应用程序:标签视图 - >导航视图 - >表视图 - >表视图 - >详细视图。我得到了一切工作(虽然我确定我正在使用managedobjectcontext子类调用混合一些fetchedresultscontroller类型调用),而我的第二个表视图显示了父表的子对象列表,尽管这是我的问题。

如果我返回到父对象并选择一个新的父对象,则新对象不会被推送到子表视图,并且显示旧子对象,直到我按下后退按钮并再次重新按下视图。这是来自父表和子表视图控制器的代码,我已经知道它们在本质上是“非规范化的”,因为有冗余调用,我只是想找到有用的东西。

我使用了Apple Recipes应用程序并使用了http://maybelost.com/2010/12/core-data-helper-revised/

中的CoreDataHelper模块

ATPTestTableViewController.h

{   NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *objectList;

    FACTypes *facTypes;
    FACTypes *selectedFACType;
}

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *objectList;
@property (nonatomic, retain) FACTypes *selectedFACType;
@property (nonatomic, retain) NSIndexPath *selectedRow;

ATPTestTableViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

self.managedObjectContext = [(ATPAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = self.managedObjectContext;

objectList = [CoreDataHelper getObjectsForEntity:@"FACTypes" withSortKey:@"number" andSortAscending:YES andContext:self.managedObjectContext];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedFACType = self.objectList[indexPath.row];
self.selectedRow = indexPath;

}

-(NSFetchedResultsController *) fetchedResultsController {
    // Set up the fetched results controller if needed.
if (fetchedResultsController == nil) {
        // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"FACTypes" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

        // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

}

return fetchedResultsController;

}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSString *segueIdentifier = [segue identifier];
if ([segueIdentifier isEqualToString:@"showDetail"]) // This can be defined via Interface Builder
{

    NSLog(@"loading segue");

    self.selectedFACType = self.objectList[self.selectedRow.row];

    ATPDetailTestTableViewController *vc = [segue destinationViewController];
    vc.managedObjectContext = self.managedObjectContext;
    vc.facType = self.objectList[self.selectedRow.row];
    NSLog(@"FACType: %@", self.selectedFACType.title);
}
}

ATPDetailTestTableViewController.h

{

    NSFetchedResultsController *fetchedResultsController;
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *objectList;

    FACTypes *facType;
    NSMutableArray *facMinimums;
    NSMutableArray *minimums;
}

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *objectList;
@property (nonatomic, retain) FACTypes *facType;
@property (nonatomic, retain) NSMutableArray *facMinimums;

ATPDetailTestTableViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

self.managedObjectContext = [(ATPAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = self.managedObjectContext;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];

NSMutableArray *sortedMinimums = [[NSMutableArray alloc] initWithArray:[self.facType.minimums allObjects]];
[sortedMinimums sortUsingDescriptors:sortDescriptors];

self.facMinimums = sortedMinimums;
[self.tableView reloadData];

}

- (NSFetchedResultsController *) fetchedResultsController {
    // Set up the fetched results controller if needed.
if (fetchedResultsController == nil) {
        // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"FACMinimums" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

        // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sortOrder" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(self.facType == %@)", self.facType];

        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

}

return fetchedResultsController;

}

2 个答案:

答案 0 :(得分:0)

尝试使用

self.tableView.indexPathForSelectedRow

而不是

self.selectedRow.row

答案 1 :(得分:0)

我认为发生的事情是当tableviewcell被选中时首先执行segue操作并将值保存在变量中,所以当它第一次导航时它没有任何传递值要显示或执行任何适当的操作,但是在第二种情况下,因为值已经存储在临时变量中,所以它们被传递。因此,它的解决方案是在self.selectedRow = indexPath之后的tableviewdidselect行中以编程方式执行segue操作,因为现在它将导航到下一个视图控制器,并相应地传递值和函数