Core data CoreDataTableViewController从segue返回时不重新加载

Core data CoreDataTableViewController从segue返回时不重新加载,core-data,uitableview,segue,Core Data,Uitableview,Segue,我有几个CoreDataTableViewController,它们利用Paul Hegarty课程中的helper类。除了这个,他们每个人都在工作,我看不出有什么不同 当表格第一次出现时,它被正确填充,并且当选择一个单元格时,segue会正确执行。但是,当我点击后退按钮时,表中到处都显示(null),(null) 我已经尝试了我能想到的每一种调用[self-useDocument]的方法,但仍然没有成功。有什么想法吗?提前谢谢 // // TeamTableViewController.m

我有几个CoreDataTableViewController,它们利用Paul Hegarty课程中的helper类。除了这个,他们每个人都在工作,我看不出有什么不同

当表格第一次出现时,它被正确填充,并且当选择一个单元格时,segue会正确执行。但是,当我点击后退按钮时,表中到处都显示(null),(null)

我已经尝试了我能想到的每一种调用[self-useDocument]的方法,但仍然没有成功。有什么想法吗?提前谢谢

//
//  TeamTableViewController.m
//

#import "TeamTableViewController.h"
#import "iTrackAppDelegate.h"
#import "CoreDataTableViewController.h"
#import "SchoolRecords.h"
#import "ScheduleViewController.h"


@interface TeamTableViewController ()
@property NSInteger toggle;

@end

@implementation TeamTableViewController

@synthesize iTrackContext = _iTrackContext;
@synthesize schoolSelected = _schoolSelected;

-(void) setSchoolSelected:(SchoolRecords *)schoolSelected
{
    _schoolSelected = schoolSelected;
}

-(void) setITrackContext:(NSManagedObjectContext *)iTrackContext
{
    if(_iTrackContext != iTrackContext){
        if (!iTrackContext) {

            MyCoreDataHandler* cdh =
            [(iTrackAppDelegate *) [[UIApplication sharedApplication] delegate] cdh];
            _iTrackContext = cdh.context;
        } else {
            _iTrackContext = iTrackContext;
        }
    }

    [self useDocument];
}


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SchoolRecords"];

    // no predicate because we want ALL the Athletes
    request.sortDescriptors = [NSArray arrayWithObjects:
                               [NSSortDescriptor sortDescriptorWithKey:@"schoolName" ascending:YES],
                               nil];

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.iTrackContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];
    __block NSInteger myCount;
    int64_t delayInSeconds = 5.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

    [self.iTrackContext performBlock:^(void){NSError* requestError = nil;
    myCount = [self.iTrackContext countForFetchRequest:request error:&requestError];
    NSLog(@"In %@ and count of iTrackContext = %lu", NSStringFromClass([self class]),(unsigned long)myCount);
    }];

    if (!myCount || myCount == 0) {
        [self displayAlertBoxWithTitle:@"No Teams" message:@"Have you added athletes yet? \nPlease go to Add Athletes" cancelButton:@"Okay"];
    }
    });
}


- (void)useDocument
{

    if (self.iTrackContext) {

        [self setupFetchedResultsController];

    } else {
        NSString* errorText = @"A problem arose opening the search results database of Athletes.";
        [self displayAlertBoxWithTitle:@"File Error" message:errorText cancelButton:@"Okay"];
    }
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];


    if (!self.iTrackContext) {

            MyCoreDataHandler* cdh =
            [(iTrackAppDelegate *) [[UIApplication sharedApplication] delegate] cdh];
            [self setITrackContext:cdh.context];

    } else {
        NSLog(@"In %@ of %@. Getting ready to call useDocument",NSStringFromSelector(_cmd), self.class);

        [self useDocument];
    }

}


- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // If divide into sections  use line below otherwise return 1.
//    return [[self.fetchedResultsController sections] count];
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 // Do not really need this with only one section, but makes code usable if add sections later.
    return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"teamInformation";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    SchoolRecords *schoolResults = [self.fetchedResultsController objectAtIndexPath:indexPath];

    NSString* titleText = schoolResults.schoolName;

    cell.textLabel.text = titleText;
    cell.detailTextLabel.text = [NSMutableString stringWithFormat:@"%@, %@", schoolResults.schoolCity, schoolResults.schoolState];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}


# pragma navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    [self setSchoolSelected:[self.fetchedResultsController objectAtIndexPath:indexPath]];

    // be somewhat generic here (slightly advanced usage)
    // we'll segue to ANY view controller that has a photographer @property

    if ([segue.identifier isEqualToString:@"scheduleDetailSegue"]) {
        // use performSelector:withObject: to send without compiler checking
        // (which is acceptable here because we used introspection to be sure this is okay)

        NSLog(@"Preparing to passing school with schoolID = %@", self.schoolSelected.schoolID);

        [segue.destinationViewController convenienceMethodForSettingSchool:self.schoolSelected];
    }
}



- (void) displayAlertBoxWithTitle:(NSString*)title message:(NSString*) myMessage cancelButton:(NSString*) cancelText
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:myMessage
                                                   delegate:nil
                                          cancelButtonTitle:cancelText
                                          otherButtonTitles:nil];
    [alert show];
}


@end

嗯,我不确定问题出在哪里。我最终删除了故事板中“有问题”的TableViewController并重新创建了它们。这就成功了。回想起来,我想知道我是否没有从我的tabViewController中指定错误的segue类型。但在我想到这种可能性之前,我删除了它