Search code examples
core-datadelegatesnspredicatensfetchedresultscontrollernsfetchrequest

Setting a predicate for different tableviews


This one has been a big problem for me, and i´m still stuck with it so i was hopping that someone could give me some kind of guidance.

What i have is:

  • 3 tableviews with multiple cells and each cell with several textfields.
  • 1 tableview that appears inside a popover every time a specific textfield on those cells is pressed. This tableview has all!! the core data methods to retrieve the necessary data from my database.

Everything works ok...but i need to distinguish what kind of data shall appear in tableview 1 or 2 or 3...So i know i have to use predicate!.

What i have done: ( and i have tried other things)

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController == nil)
    {

        NSFetchRequest *fetchRequestList = [[NSFetchRequest alloc] init];
        NSEntityDescription *entityList = [NSEntityDescription entityForName:@"List"     inManagedObjectContext:self.managedObjectContext];
        [fetchRequestLista setEntity:entityList];

        TableViewOne *table1 = [[Cobertura alloc]init];
        TableViewTwo *table2 = [[Cobertura alloc]init];

        if (table1 textFieldShouldBeginEditing:table1.textFieldPressed)
        {
            fetchRequestList.predicate = [NSPredicate predicateWithFormat:@"%K IN %@", @"reference",     arrayTableview1];
        }

        if (table2 textFieldShouldBeginEditing:table2.textFieldPressed)
        {
            fetchRequestList.predicate = [NSPredicate predicateWithFormat:@"%K IN %@", @"reference",    arrayTableview2];
        }

        NSSortDescriptor *cellTitle = [[NSSortDescriptor alloc] initWithKey:@"reference"     ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:cellTitle, nil];
        [fetchRequestLista setSortDescriptors:sortDescriptors];

        _fetchedResultsController = [[NSFetchedResultsController alloc]     initWithFetchRequest:fetchRequestLista managedObjectContext:self.managedObjectContext     sectionNameKeyPath:@"referencia" cacheName:nil];
        _fetchedResultsController.delegate = self;
        self.fetchedResultsController = _fetchedResultsController;

        return _fetchedResultsController;
    }

In each of my tableviews, i have an instance of the "popoverTableview" in my method textFieldShouldBeginEditing:

popoverTableview = [self.storyboard     instantiateViewControllerWithIdentifier:@"popoverTableview"];
popover = [[UIPopoverController alloc] initWithContentViewController:popoverTableview];
[popover presentPopoverFromRect:textField.bounds inView:textField permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
popoverTableview.delegate = self;
popoverTableview.popView = self.popover; 

So, if i´m in tableview1 i need to get [NSPredicate predicateWithFormat:@"%K IN %@", @"reference", arrayTableview1];

Should i be creating some kind of method that my tableviewS could access? What am i forgetting here or not paying attention?

Thanks in advance, and any kind of advise would be welcome! Regards

For everyone that was experiencing the same problem that i was, here is what i have done to resolve:

This is when i´m creating the popoverview when a specific textfield is pressed:

popoverTableview = [self.storyboard      instantiateViewControllerWithIdentifier:@"popoverTableview"]initWithTextFieldTag:myTextFieldThatWasPressed.tag]
popover = [[UIPopoverController alloc] initWithContentViewController:popoverTableview];
[popover presentPopoverFromRect:textField.bounds inView:textField     permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
popoverTableview.delegate = self;
popoverTableview.popView = self.popover;
popoverTableview.aIntVariable = myTextFieldThatWasPressed;

then in my popovertableview:

- (id)initWithTextFieldTag:(int)textFieldTag
{
self.aIntVariable = textFieldTag;

return self;
}

Then in the fetchedResultsController method, you´ll just have to create simple if´s telling wich predicate you want...

Regards


Solution

  • If the fetched results controller is for the popover table and you need to know in which table the text field was selected, I'd recommend tagging each of the text fields when you create them and creating an int _currentTable instance variable. That way, when your textFieldShouldBeginEditing: method is called, you can set the ivar's value with the tag and check that tag when creating the fetched results controller for the popover table.

    So, instead of

    if (table1 textFieldShouldBeginEditing:table1.textFieldPressed)
    {
        fetchRequestList.predicate = [NSPredicate predicateWithFormat:@"%K IN %@", @"reference",     arrayTableview1];
    }
    
    if (table2 textFieldShouldBeginEditing:table2.textFieldPressed)
    {
        fetchRequestList.predicate = [NSPredicate predicateWithFormat:@"%K IN %@", @"reference",    arrayTableview2];
    }
    

    you'll have

    if (_currentTable == 1) {
        fetchRequestList.predicate = // table one predicate
    } else if (_currentTable == 2) {
        fetchRequestList.predicate = // table two predicate
    }
    

    UPDATE:

    This is how I would override the init from code. In your popover table view controller implementation:

    - (id)initWithTableTag:(int)tableTag
    {
        self = [super init];
        if (self) {
            _currentTable = tableTag;
        }
        return self;
    }
    

    (Make sure you also declare - (id)initWithTableTag:(int)tableTag; in your header.) Then, when you create and present the popover controller (which I'm assuming you're doing in the textFieldShouldBeginEditing: delegate call):

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
        // ...
    
        YourPopoverTableViewControllerClass *vc = [[YourPopoverTableViewControllerClass alloc] initWithTableTag:textField.tag];
    
        // ...
        // display the popover
    
        return YES;
    }
    

    Unfortunately, I don't know how to do this using storyboards.