I have several CoreDataTableViewControllers that utilize the helper class from Paul Hegarty's course. Everyone of them works except this one, and I cannot see a difference.

When the table first comes up, it is correctly populated and the segue executes properly when a cell is selected. However when I hit the back button, the table displays (null), (null) everywhere.

I have tried every variant of calling [self useDocument] that I can think of, still to no avail. Any thoughts? Thanks in advance.

//
//  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
有帮助吗?

解决方案

Well, I am not certain what the problem was. I ended up deleting the "offending" TableViewControllers in StoryBoard and recreated them. That did the trick. In retrospect, I wonder if I did not specify the wrong type of segue from my tabViewController. But I deleted it before I thought of that possibility.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top