Skip to content

Commit

Permalink
search working
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchiles committed Apr 14, 2012
1 parent e38dbe6 commit b894317
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 76 deletions.
Binary file modified .DS_Store
Binary file not shown.
12 changes: 11 additions & 1 deletion OPECategoryViewController.h
Expand Up @@ -9,6 +9,16 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import "OPETypeViewController.h" #import "OPETypeViewController.h"


@interface OPECategoryViewController : UITableViewController @interface OPECategoryViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchDisplayDelegate, UISearchBarDelegate>

@property (nonatomic,strong) IBOutlet UITableView * mainTableView;
@property (nonatomic,strong) UISearchDisplayController * searchDisplayController;
@property (nonatomic,strong) IBOutlet UISearchBar * searchBar;
@property (nonatomic,strong) NSDictionary * categoriesAndTypes;
@property (nonatomic,strong) NSDictionary * types;

@property (nonatomic, strong) id <PassCategoryAndType> delegate;

@property (nonatomic, retain) NSMutableArray *searchResults;


@end @end
92 changes: 73 additions & 19 deletions OPECategoryViewController.m
Expand Up @@ -11,15 +11,9 @@


@implementation OPECategoryViewController @implementation OPECategoryViewController



@synthesize mainTableView,searchBar,searchDisplayController;
- (id)initWithStyle:(UITableViewStyle)style @synthesize categoriesAndTypes,searchResults,types;
{ @synthesize delegate;
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}


- (void)didReceiveMemoryWarning - (void)didReceiveMemoryWarning
{ {
Expand All @@ -39,7 +33,21 @@ - (void)viewDidLoad
// self.clearsSelectionOnViewWillAppear = NO; // self.clearsSelectionOnViewWillAppear = NO;


// Uncomment the following line to display an Edit button in the navigation bar for this view controller. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

// self.navigationItem.rightBarButtonItem = self.editButtonItem; // self.navigationItem.rightBarButtonItem = self.editButtonItem;
OPETagInterpreter * tagInterpreter = [OPETagInterpreter sharedInstance];
categoriesAndTypes = tagInterpreter.categoryAndType;
NSMutableDictionary * tempTypes = [[NSMutableDictionary alloc] init];
for (NSString * category in categoriesAndTypes)
{
for (NSString * type in [categoriesAndTypes objectForKey:category]) {
[tempTypes setObject:category forKey:type];
}

}
types = [tempTypes copy];
//NSLog(@"Types: %@",types);

} }


- (void)viewDidUnload - (void)viewDidUnload
Expand Down Expand Up @@ -75,6 +83,25 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
return (interfaceOrientation == UIInterfaceOrientationPortrait); return (interfaceOrientation == UIInterfaceOrientationPortrait);
} }


- (void)handleSearchForTerm:(NSString *)searchTerm
{
searchResults = [[NSMutableArray alloc] init];
if ([searchTerm length] != 0)
{
for (NSString * currentString in types)
{
//NSLog(@"CurrentString: %@",currentString);
if ([currentString rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location != NSNotFound)
{
NSLog(@"Match");
NSDictionary * match = [[NSDictionary alloc] initWithObjectsAndKeys:currentString,@"type",[types objectForKey:currentString],@"category", nil];
[searchResults addObject:match];

}
}
}
}

#pragma mark - Table view data source #pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
Expand All @@ -86,22 +113,28 @@ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ {
// Return the number of rows in the section. // Return the number of rows in the section.
OPETagInterpreter * tagInterpreter = [OPETagInterpreter sharedInstance]; if (tableView == [[self searchDisplayController] searchResultsTableView]) {
return [tagInterpreter.categoryAndType count];
return [searchResults count];
}
return [categoriesAndTypes count];
//return 0; //return 0;
} }


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

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


OPETagInterpreter * tagInterpreter = [OPETagInterpreter sharedInstance]; if (tableView == [[self searchDisplayController] searchResultsTableView]) {
NSArray * categories = [[tagInterpreter.categoryAndType allKeys]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; cell.textLabel.text = [[self.searchResults objectAtIndex:indexPath.row] objectForKey:@"type"];
return cell;
}

NSArray * categories = [[categoriesAndTypes allKeys]sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];


cell.textLabel.text = [categories objectAtIndex:indexPath.row]; cell.textLabel.text = [categories objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Expand Down Expand Up @@ -159,15 +192,36 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
// Pass the selected object to the new view controller. // Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES]; [self.navigationController pushViewController:detailViewController animated:YES];
*/ */
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (tableView == [[self searchDisplayController] searchResultsTableView]) {
if (!delegate) {
NSLog(@"delegate is nil");
}
[[self delegate] setCategoryAndType: [searchResults objectAtIndex:indexPath.row]];
[self.navigationController popViewControllerAnimated:YES];
}
else {
OPETypeViewController * viewer = [[OPETypeViewController alloc] initWithNibName:@"OPETypeViewController" bundle:nil];
viewer.title = @"Type";
viewer.category = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[viewer setDelegate: [[[self navigationController] viewControllers] objectAtIndex:1]];
[self.navigationController pushViewController:viewer animated:YES];
}



OPETypeViewController * viewer = [[OPETypeViewController alloc] initWithNibName:@"OPETypeViewController" bundle:nil];
viewer.title = @"Type";
viewer.category = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
[viewer setDelegate: [[[self navigationController] viewControllers] objectAtIndex:1]];






[self.navigationController pushViewController:viewer animated:YES];
} }


#pragma mark - search delegate
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self handleSearchForTerm:searchString];

return YES;
}


@end @end

0 comments on commit b894317

Please sign in to comment.