0

enter image description hereI looked through the apple tutorial, and I am trying to make the simplified version for an array of words.

Essentially when a user starts typing the letter a, I want to dynamically show all the matches in the array that start with a, and so forth:

array 1 = auto, airplane, air, airy, beans, chairs

start typing a: auto airplane air airy

au: auto

this is my code, but i get an error at shouldReloadTableForSearchString on the first method

Tank you in advance

#import "otherSearchViewController.h"
#import "SearchViewController.h"
#import "AppDelegate.h"

@interface itemSearchViewController ()


@end

@implementation itemSearchViewController


@synthesize listContent, filteredListContent, savedSearchTerm, savedScopeButtonIndex, searchWasActive;
@synthesize delegate;

/*
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark -
#pragma mark Lifecycle methods
*/

- (void)viewDidLoad
{

    //[super viewDidLoad];

    self.title = @"Search";

    NSString *path =[[NSBundle mainBundle] pathForResource:@"possibleItems" ofType:@"plist"];
    NSArray *content = [NSArray arrayWithContentsOfFile:path];
    listContent =[NSArray arrayWithArray:content];
    if([content count] == 0)

    {
        NSLog(@"nsma is empty");
    }
    NSLog(@"list contents%@", listContent);


   // NSLog(@"list content = %@", listContent);
        // create a filtered list that will contain products for the search results table.

    self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.listContent count]];

        // restore search settings if they were saved in didReceiveMemoryWarning.
    if (self.savedSearchTerm)
        {
        [self.searchDisplayController setActive:self.searchWasActive];
        [self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex];
        [self.searchDisplayController.searchBar setText:savedSearchTerm];

        self.savedSearchTerm = nil;
        }

    [self.tableView reloadData];
    self.tableView.scrollEnabled = YES;
}

- (void)viewDidUnload
{
        //causing problem so disabled
    //self.filteredListContent = nil;
}

- (void)viewDidDisappear:(BOOL)animated
{
        // save the state of the search UI so that it can be restored if the view is re-created
    self.searchWasActive = [self.searchDisplayController isActive];
    self.savedSearchTerm = [self.searchDisplayController.searchBar text];
    self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex];
}

- (void)dealloc
{
    [listContent release];
    [filteredListContent release];

    [super dealloc];
}


#pragma mark -
#pragma mark UITableView data source and delegate methods


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    /*
     If the requesting table view is the search display controller's table view, return the count of
     the filtered list, otherwise return the count of the main list.
     */
    if (tableView == self.searchDisplayController.searchResultsTableView)
        {
        return [self.filteredListContent count];
        }
    else
        {
        return [self.listContent count];
        }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *kCellID = @"cellID";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
    if (cell == nil)
        {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease];
        }

    /*
     If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list.
     */
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    /*
     If the requesting table view is the search display controller's table view, configure the next view controller using the filtered content, otherwise use the main list.
     */

}

#pragma mark -
#pragma mark Content Filtering

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    /*
     Update the filtered array based on the search text and scope.
     */

    [self.filteredListContent removeAllObjects]; // First clear the filtered array.

    /*
     Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
     */

    for (searchText in listContent){
        if ([searchText isEqualToString:scope])
            {
            NSComparisonResult result = [scope compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];

            if (result == NSOrderedSame)
                {
                [self.filteredListContent addObject:scope];
                }
            }
        }
}


#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods


//when you start typing you get this
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

        // Return YES to cause the search result table view to be reloaded.
    return YES;
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

        // Return YES to cause the search result table view to be reloaded.
    return YES;
}

@end
William Falcon
  • 9,813
  • 14
  • 67
  • 110
  • What's the error it's giving you? – Miles Alden Dec 01 '12 at 15:52
  • exc_bad_access posted the picture just now – William Falcon Dec 01 '12 at 16:00
  • I guess scope is the general categories apple uses in its tutorial. I dont have that, so what don't I need here? – William Falcon Dec 01 '12 at 16:07
  • Either `searchString` or the pointer you passed as the `scope` parameter are nil or are bad pointers (point to unallocated memory). You could break out the expression that generates the scope and see in the debugger if any part of it returns nil (assuming `searchString` isn't already nil). – user1118321 Dec 01 '12 at 16:16
  • you are right, for some reason the array I create in viewdidload does not have anything in it by the time we get to this method. How can I fix that?. Additionally how do I load the results in cells below as they are populated? – William Falcon Dec 01 '12 at 16:50
  • The array is probably autoreleasing. Instead of using the class methods to create them, use the instance methods, i.e., replace `NSArray *content = [NSArray arrayWithContentsOfFile:path];` with `NSArray *content = [[NSArray alloc] initWithContentsOfFile:path];` – Miles Alden Dec 01 '12 at 17:12

2 Answers2

1

The array is probably autoreleasing. Instead of using the class methods to create them, use the instance methods, i.e., replace NSArray *content = [NSArray arrayWithContentsOfFile:path]; with NSArray *content = [[NSArray alloc] initWithContentsOfFile:path];

Miles Alden
  • 1,542
  • 1
  • 11
  • 21
  • if you get a chance look at part two of this http://stackoverflow.com/questions/13661746/load-results-on-table-while-searching – William Falcon Dec 01 '12 at 18:18
0

it turned out to be the faulty array

William Falcon
  • 9,813
  • 14
  • 67
  • 110