Created
December 5, 2014 04:05
-
-
Save sergiorobles198/d35c6a40bc531860d242 to your computer and use it in GitHub Desktop.
Sample UISearchController Project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| @interface CustomTableViewCell : UITableViewCell | |
| @property (strong, nonatomic) IBOutlet UILabel *videoLabel; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "CustomTableViewCell.h" | |
| @implementation CustomTableViewCell | |
| @synthesize videoLabel = _videoLabel; | |
| - (void)awakeFromNib { | |
| // Initialization code | |
| } | |
| - (void)setSelected:(BOOL)selected animated:(BOOL)animated { | |
| [super setSelected:selected animated:animated]; | |
| // Configure the view for the selected state | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| @interface MainTableViewController : UIViewController | |
| @property (strong, nonatomic) IBOutlet UITableView *tableView; | |
| @property (strong, nonatomic) NSMutableArray *tableData; | |
| @property (retain, nonatomic) NSMutableArray *arrayOfVideosLinks; | |
| @property (nonatomic, retain) NSMutableString *videoUrl; | |
| @property (nonatomic, retain) NSMutableString *htmlString; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "MainTableViewController.h" | |
| #import "CustomTableViewCell.h" | |
| #import "SecondaryTableViewController.h" | |
| #import "SearchResultsTableViewController.h" | |
| @interface MainTableViewController () <UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating> | |
| @property (nonatomic, strong) UISearchController *searchController; | |
| @property (nonatomic, strong) SearchResultsTableViewController *resultsTableController; | |
| @property (nonatomic, strong) NSMutableArray *searchResults; | |
| @property BOOL searchControllerWasActive; | |
| @property BOOL searchControllerSearchFieldWasFirstResponder; | |
| @end | |
| @implementation MainTableViewController | |
| - (void)viewDidLoad { | |
| [super viewDidLoad]; | |
| self.title = @"Videos"; | |
| self.tableData = [NSMutableArray arrayWithObjects:@"First",@"Second",@"Third",nil]; | |
| _resultsTableController = [[SearchResultsTableViewController alloc] init]; | |
| _searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController]; | |
| self.searchController.searchResultsUpdater = self; | |
| [self.searchController.searchBar sizeToFit]; | |
| self.tableView.tableHeaderView = self.searchController.searchBar; | |
| self.searchController.delegate = self; | |
| self.searchController.dimsBackgroundDuringPresentation = NO; | |
| self.searchController.searchBar.delegate = self; | |
| self.definesPresentationContext = YES; | |
| //Videos | |
| self.arrayOfVideosLinks=[NSMutableArray arrayWithObjects: | |
| [NSString stringWithFormat:@"Kt4f7_8Fpe4"], | |
| [NSString stringWithFormat:@"XOWhGWLnAIA"], | |
| [NSString stringWithFormat:@"FFNWlvmOvE4"], | |
| nil]; | |
| } | |
| - (void)viewDidAppear:(BOOL)animated { | |
| [super viewDidAppear:animated]; | |
| if (self.searchControllerWasActive) { | |
| self.searchController.active = self.searchControllerWasActive; | |
| _searchControllerWasActive = NO; | |
| if (self.searchControllerSearchFieldWasFirstResponder) { | |
| [self.searchController.searchBar becomeFirstResponder]; | |
| _searchControllerSearchFieldWasFirstResponder = NO; | |
| } | |
| } | |
| } | |
| #pragma mark - UISearchBarDelegate | |
| - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { | |
| [searchBar resignFirstResponder]; | |
| } | |
| #pragma mark - UISearchControllerDelegate | |
| - (void)presentSearchController:(UISearchController *)searchController { | |
| } | |
| - (void)willPresentSearchController:(UISearchController *)searchController { | |
| //NSLog(@"willPresentSearchController"); | |
| } | |
| - (void)didPresentSearchController:(UISearchController *)searchController { | |
| //NSLog(@"didPresentSearchController"); | |
| } | |
| - (void)willDismissSearchController:(UISearchController *)searchController { | |
| //NSLog(@"willDismissSearchController"); | |
| } | |
| - (void)didDismissSearchController:(UISearchController *)searchController { | |
| //NSLog(@"didDismissSearchController"); | |
| } | |
| #pragma mark - UITableViewDelegate | |
| - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
| { | |
| // Return the number of sections. | |
| return 1; | |
| } | |
| - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
| { | |
| return self.tableData.count; | |
| } | |
| - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| static NSString *simpleTableIdentifier = @"CustomTableViewCell"; | |
| CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; | |
| if (cell == nil) | |
| { | |
| NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil]; | |
| cell = [nib objectAtIndex:0]; | |
| } | |
| cell.videoLabel.text = [_tableData objectAtIndex:indexPath.row]; | |
| return cell; | |
| } | |
| -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| SecondaryTableViewController *secondaryVC = [[SecondaryTableViewController alloc] init]; | |
| secondaryVC.videoUrl = self.arrayOfVideosLinks[indexPath.row]; | |
| [self.navigationController pushViewController:secondaryVC animated:YES]; | |
| [tableView deselectRowAtIndexPath:indexPath animated:NO]; | |
| } | |
| - (void)didReceiveMemoryWarning { | |
| [super didReceiveMemoryWarning]; | |
| // Dispose of any resources that can be recreated. | |
| } | |
| #pragma mark - UISearchResultsUpdating | |
| - (void)updateSearchResultsForSearchController:(UISearchController *)searchController { | |
| NSString *searchText = searchController.searchBar.text; | |
| NSString *strippedStr = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; | |
| NSArray *searchItems = nil; | |
| if (strippedStr.length > 0) { | |
| searchItems = [strippedStr componentsSeparatedByString:@" "]; | |
| } | |
| NSMutableArray *andMatchPredicates = [NSMutableArray array]; | |
| if ( [searchText length] == 0 ) { | |
| [self.searchResults addObjectsFromArray:self.tableData]; | |
| } else { | |
| for (NSString *searchString in self.tableData) { | |
| NSMutableArray *searchItemsPredicate = [NSMutableArray array]; | |
| NSExpression *lhs = [NSExpression expressionForKeyPath:@"title"]; | |
| NSExpression *rhs = [NSExpression expressionForConstantValue:searchString]; | |
| NSPredicate *finalPredicate = [NSComparisonPredicate | |
| predicateWithLeftExpression:lhs | |
| rightExpression:rhs | |
| modifier:NSDirectPredicateModifier | |
| type:NSContainsPredicateOperatorType | |
| options:NSCaseInsensitivePredicateOption]; | |
| [searchItemsPredicate addObject:finalPredicate]; | |
| NSCompoundPredicate *orMatchPredicates = (NSCompoundPredicate *)[NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate]; | |
| [andMatchPredicates addObject:orMatchPredicates]; | |
| } | |
| } | |
| NSCompoundPredicate *finalCompoundPredicate = nil; | |
| finalCompoundPredicate = | |
| (NSCompoundPredicate *)[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates]; | |
| _searchResults = [[_searchResults filteredArrayUsingPredicate:finalCompoundPredicate] mutableCopy]; | |
| SearchResultsTableViewController *tableController = (SearchResultsTableViewController *)self.searchController.searchResultsController; | |
| tableController.searchResults = _searchResults; | |
| [self.tableView reloadData]; | |
| } | |
| #pragma mark - UIStateRestoration | |
| static NSString *ViewControllerTitleKey = @"ViewControllerTitleKey"; | |
| static NSString *SearchControllerIsActiveKey = @"SearchControllerIsActiveKey"; | |
| static NSString *SearchBarTextKey = @"SearchBarTextKey"; | |
| static NSString *SearchBarIsFirstResponderKey = @"SearchBarIsFirstResponderKey"; | |
| - (void)encodeRestorableStateWithCoder:(NSCoder *)coder { | |
| [super encodeRestorableStateWithCoder:coder]; | |
| [coder encodeObject:self.title forKey:ViewControllerTitleKey]; | |
| UISearchController *searchController = self.searchController; | |
| BOOL searchDisplayControllerIsActive = searchController.isActive; | |
| [coder encodeBool:searchDisplayControllerIsActive forKey:SearchControllerIsActiveKey]; | |
| if (searchDisplayControllerIsActive) { | |
| [coder encodeBool:[searchController.searchBar isFirstResponder] forKey:SearchBarIsFirstResponderKey]; | |
| } | |
| [coder encodeObject:searchController.searchBar.text forKey:SearchBarTextKey]; | |
| } | |
| - (void)decodeRestorableStateWithCoder:(NSCoder *)coder { | |
| [super decodeRestorableStateWithCoder:coder]; | |
| self.title = [coder decodeObjectForKey:ViewControllerTitleKey]; | |
| _searchControllerWasActive = [coder decodeBoolForKey:SearchControllerIsActiveKey]; | |
| _searchControllerSearchFieldWasFirstResponder = [coder decodeBoolForKey:SearchBarIsFirstResponderKey]; | |
| self.searchController.searchBar.text = [coder decodeObjectForKey:SearchBarTextKey]; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| @interface SearchResultsTableViewController : UITableViewController | |
| @property (strong, nonatomic) NSMutableArray *searchResults; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "SearchResultsTableViewController.h" | |
| #import "MainTableViewController.h" | |
| #import "SecondaryTableViewController.h" | |
| #import "CustomTableViewCell.h" | |
| @implementation SearchResultsTableViewController | |
| - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
| return self.searchResults.count; | |
| } | |
| - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| static NSString *simpleTableIdentifier = @"CustomTableViewCell"; | |
| CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; | |
| if (cell == nil) | |
| { | |
| NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil]; | |
| cell = [nib objectAtIndex:0]; | |
| } | |
| cell.videoLabel.text = [_searchResults objectAtIndex:indexPath.row]; | |
| return cell; | |
| } | |
| -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
| { | |
| SecondaryTableViewController *secondaryVC = [[SecondaryTableViewController alloc] init]; | |
| secondaryVC.videoUrl = self.searchResults[indexPath.row]; | |
| [self.navigationController pushViewController:secondaryVC animated:YES]; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| @interface SecondaryTableViewController : UIViewController | |
| @property (strong, nonatomic) IBOutlet UIWebView *videoView; | |
| @property (strong, nonatomic) NSMutableString *videoUrl; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "SecondaryTableViewController.h" | |
| @interface SecondaryTableViewController () | |
| @end | |
| @implementation SecondaryTableViewController | |
| - (void)viewDidLoad { | |
| [super viewDidLoad]; | |
| // Do any additional setup after loading the view from its nib. | |
| [self.videoView setAllowsInlineMediaPlayback:YES]; | |
| [self.videoView setMediaPlaybackRequiresUserAction:YES]; | |
| NSString *embedHTML = [NSString stringWithFormat:@"\ | |
| <html><head>\ | |
| <style type=\"text/css\">\ | |
| body {\ | |
| background-color: transparent;\ | |
| color: blue;\ | |
| }\ | |
| </style>\ | |
| </head><body style=\"margin:0\">\ | |
| <iframe height=\"667\" width=\"380\" src=\"http://www.youtube.com/embed/%@\"></iframe>\ | |
| </body></html>",_videoUrl]; | |
| [self.videoView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]]; | |
| } | |
| - (void)didReceiveMemoryWarning { | |
| [super didReceiveMemoryWarning]; | |
| // Dispose of any resources that can be recreated. | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment