0

I am trying to reload my tableview with the parameters received from changing a UIDatepicker. Currently I have the following method that captures the data and sends it to the UIButton (which works). I am trying to reset the container view that houses the UITableViewController which is called DisplayTableViewController but it is not reloading the NSFetchController. Any suggestions about how to debug or fix this issue?

-(void)changeDate:(UIDatePicker *)sender {

    //NSDate Formatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"ddMMyyyy"];

    //Convert to string
    NSString *stringFromDate = [dateFormatter stringFromDate:sender.date];

    //Send to UIButton
    [dateLabel setTitle:stringFromDate forState:UIControlStateNormal];

    DisplayTableViewController *tbc = (DisplayTableViewController *)self.childViewControllers[0];
    [tbc.tableView reloadData];

}

enter image description here


EDIT

 if ([[segue identifier] isEqualToString:@"ShowDisplayResults"])
    {
        //Format Date for Seague
        NSDateFormatter *todaysdate = [[NSDateFormatter alloc] init];
        [todaysdate setDateFormat:@"ddMMyyyy"];
        //Convert to string for Seague
        NSString *stringFromDate = [todaysdate stringFromDate:[NSDate date]];
        // Store the text we entered to dateSeague
        dateSeague =stringFromDate;
        // Get reference to the destination view controller
        DisplayTableViewController *targetVC = [segue destinationViewController];
        // Pass any objects to the view controller here, like...
        targetVC.dateSeague = dateSeague;
        targetVC.Ex = self.Ex;

    }

NSFETCHCONTROLLER IN CONTAINER

- (NSFetchedResultsController *)fetchedResultsController {
    NSLog(@"Test Date for second seague = %@", dateSeague);
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    //Query Entity
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WorkoutDetails"inManagedObjectContext:managedObjectContext];
    //Fetch Entity
    [fetchRequest setEntity:entity];

    //Sort Returned Values
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"weight" ascending:NO];
    //Fetch Sorted Array
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    //Set Return Size
    [fetchRequest setFetchBatchSize:20];

    //Predicate Results Based on ID
    NSPredicate *sortName = [NSPredicate predicateWithFormat:@"exid == %@", Ex.name];
    //Get NSDate format from String
    NSString *stringFromDate = dateSeague;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    //Convert Back to NSDate
    [df setDateFormat:@"ddMMyyyy"];
    NSDate *inputedDate = [df dateFromString: stringFromDate];
    //Set Predicate
    NSPredicate *sortDate = [NSPredicate predicateWithFormat:@"date == %@", inputedDate];
    //Compound Predicate Array
    NSPredicate *placesPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:sortName, sortDate, nil]];
    //Fetch Predicate Compound Array
    [fetchRequest setPredicate:placesPredicate];
    //Continue Fetching!
    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext sectionNameKeyPath:nil
                                                   cacheName: nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}
memyselfandmyiphone
  • 1,080
  • 4
  • 21
  • 43

2 Answers2

0

First of all self.childViewControllers[0] will get you the first view controller on the view controller hierarchy and you want this only if you have one view controller that contains another view controller.

Second it's not a good practice to use this approach since you don't really know the state of the view controller when you call this and it's possible that the view controller at that index won't be the same class as you expected.

So I suggest that you implement a delegate for your view controller or view that holds the date picker and when the date is changed and you do all the things that you want to do with the date, call the delegate. If you don't want to do anything with the date, you just want to pass it to the view controller, then you can set the date picker delegate to be your DisplayTableViewController

As a side note, I don't know the hierarchy that you have right now and if you don't find my answer helpful please add more details to your question (the hierarchy and the flow that you want to get)

danypata
  • 9,895
  • 1
  • 31
  • 44
  • Hi @danypata I am passing the value of the uibutton to the NSFetchController within the second UITableViewController through a simple string. When I input a data the UITable reloads and updates but when I change the date it does not update. – memyselfandmyiphone Jul 12 '13 at 21:32
  • I have added a image above to show. The NSPredicate in the second View contains the string value of the UIbutton – memyselfandmyiphone Jul 12 '13 at 21:33
  • Do a check to your value that is passed to the fetchController, see if is ok, also check if the data returned by the fetchController is not nil(or empty). If all of the above are ok, then check where are you calling `reloadData` for table view. – danypata Jul 12 '13 at 21:35
  • Ah when I change the date it come back as NIL however when it first loads it shows the date in the string – memyselfandmyiphone Jul 12 '13 at 21:39
  • So, how do you set the date to your `DisplayTableViewController ` in order to use the selected date ? – danypata Jul 12 '13 at 21:40
  • Code updated above. So it looks like the values are being lost when the date is changed on the UIDatePicker. – memyselfandmyiphone Jul 12 '13 at 21:47
  • Use `self.dateSeague` not `dateSeague` because it is a property. Also call the `reloadData` in your `viewDidLoad` method of the view controller that contains the table view ;) – danypata Jul 12 '13 at 21:49
  • It's perfectly acceptable to use self.childViewControllers[0] to access the one child he has -- he hooked it up, he knows there's one and he knows the class. Using a delegate wouldn't be any different. How would you reference that child controller to set yourself as the delegate without using self.childViewControllers[0]? – rdelmar Jul 12 '13 at 22:05
  • I didn't know at the time the hierarchy, and if you want to set the child as the delegate you can do it when you create the child and push/present ;) – danypata Jul 12 '13 at 22:07
  • First call the method for fetch request setup and then `reloadData` – danypata Jul 12 '13 at 23:06
0

Are query params for the NSFetchRequest of the NSFetchController changing? If so, make sure that you use deleteCacheWithName if it is using a cache, and then reconfigure the NSFetchController.

KHansenSF
  • 604
  • 4
  • 7
  • I am using `NSpredicates` within the `NSFetchRequest` however I am not using a catch and have it set as `cacheName: nil`. When I update the table by entering data it updates in real time but when I change the value date (which is passed through a string to the NSpredicate) it does not reload the query – memyselfandmyiphone Jul 12 '13 at 21:35
  • The code you specified above calls reloadTable. Where is the FRC reconfigured? That is not automatic. – KHansenSF Jul 12 '13 at 21:37
  • I just did a slight debug there and when I first load the NSFetchRequest it get the value of the date, however when I change the date using the date picker it comes back as nil – memyselfandmyiphone Jul 12 '13 at 21:40
  • Perhaps it would be best if you posted the code where you configure you FRC – KHansenSF Jul 12 '13 at 21:43
  • Code updated above. So it looks like the values are being lost when the date is changed on the UIDatePicker. All the value are. – memyselfandmyiphone Jul 12 '13 at 21:47
  • You need to update tbc.dateSeague with the new date in your code above. But then you need to call the code that configures your FRC rather than simply reloadData – KHansenSF Jul 12 '13 at 21:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/33393/discussion-between-khansensf-and-albaclan) – KHansenSF Jul 12 '13 at 21:51