Search code examples
ioswebviewplisttableviewsearchable

Load only tagged items from plist to UITableView


Is there a specific way to load only certain tagged items from a plist file to a UITableView? Something like: load this item into a cell if the property "Tag" contains "transportation" in it, keeping in mind that the property "Tag" will will be string with "transportation, car, train, bus, etc."

Because of what I'm working with (HTML data loading in UIWebViews), I haven't found a method that allows me to use true search (without paying), so I was planning on implementing a tableview with all "searchable" keywords which will then load only certain items into the tableview, form which the user can get more information in the form of webview.


Solution

  • I figured out how to use NSPredicates and loaded the table.

    (void)viewWillAppear:(BOOL)animated
    {
    // Refresh the contents of the table whenever this screen becomes visible
    self.favReps = [[[NSUserDefaults standardUserDefaults] objectForKey:@"favReps"]mutableCopy];
    NSString *contentFile = [[NSBundle mainBundle] pathForResource:@"cf10" ofType:@"plist"];
    NSArray *Contents = [[NSArray alloc] initWithContentsOfFile:contentFile];
    NSPredicate *inPredicate = [NSPredicate predicateWithFormat:@"Section IN%@", favReps];
    favRepsDict = [[[NSMutableArray alloc] initWithArray:[Contents filteredArrayUsingPredicate:inPredicate]] mutableCopy];
    
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    }
    

    This will update every time you come back to the favorites screen.