0

Can anyone help me? I have created a dictionary with the contents of a plist(data.plist) file but now I cannot figure out how to search and display the contents. I have searched online for days and cannot get a resolution.

Most tutorials show a pre-defined array being used but if I change it to my plist it crashes.

Any help would be greatly appreciated.

2
Contributors
4
Replies
48
Views
2 Years
Discussion Span
Last Post by Shane-dev
Featured Replies
  • 1

    You need to post your code before we can help you. Just generalities like you posted are not helpful to determine the cause of your problem. Read More

1

You need to post your code before we can help you. Just generalities like you posted are not helpful to determine the cause of your problem.

0

Ah, okay. Sorry I didn't know whether or not that was frowned upon.

//
//  ViewController.h
//  Search
//
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate>

@end

Here is my .m file as well.

//
//  ViewController.m
//  Search
//
//

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) NSArray *array;
@property (strong, nonatomic) NSArray *arrayCount;
@property (strong, nonatomic) NSArray *searchResults;
@property (strong, nonatomic) NSMutableArray *filteredTableData;

@end

@implementation ViewController

int arrayCounter = 0;
NSString *test = @"";


- (void)viewDidLoad
{
    [super viewDidLoad];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];

    NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];


    self.arrayCount = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];
    NSLog(@"Array size: %d", [self.arrayCount count]);

    //self.array = [[NSArray alloc] initWithObjects: @"Test", @"Test 2", @"Test 3", nil];

    self.array = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"data" ofType:@"plist"]];

    self.searchResults = [[NSArray alloc] init];

}

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

#pragma Table View Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchResults count];

    } else {
        return [self.array count];
    }
//    return [self.array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];

    } else {
        cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
    }
//    cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
    return cell;
}

#pragma Search Methods

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", searchText];
//    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"contains[c] %@", searchText];
    self.searchResults = [self.array filteredArrayUsingPredicate:predicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

@end

Data.plist:
Root
Item 0 - Dictionary
Cell - String ( Bob )
Item 1 - Dictionary
Cell - String ( Bob 2 )
Item 2 - Dictionary
Cell - String ( Bob 3 )

And see if I populate the array with objects instead of withcontents of file it functions fine and populates the results.

Thanks for looking into this :)

Edited by Shane-dev: Make it easier to read

0

I guess thanks anyways. I will go ahead and create a new post to donate again to see if I can gain further attention.

This article has been dead for over six months. Start a new discussion instead.
Be sure to adhere to our posting rules.