Created
December 7, 2012 22:59
-
-
Save quique123/4237242 to your computer and use it in GitHub Desktop.
UISearchBar
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
// | |
// SecondViewController.m | |
// FarsimanTab | |
// | |
// Created by Marcio Valenzuela on 11/26/12. | |
// Copyright (c) 2012 Marcio Valenzuela. All rights reserved. | |
// | |
#import "SecondViewController.h" | |
#import "SDCoreDataController.h" | |
#import "TableViewCell.h" | |
#import "Holiday.h" | |
#import "Birthday.h" | |
#import "SDSyncEngine.h" | |
@interface SecondViewController () | |
@property (nonatomic, strong) NSDateFormatter *dateFormatter; | |
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext; | |
@end | |
@implementation SecondViewController | |
@synthesize dateFormatter; | |
@synthesize managedObjectContext; | |
@synthesize entityName; | |
@synthesize refreshButton; | |
@synthesize dates; | |
@synthesize filteredResultsArray; | |
@synthesize resultsSearchBar; | |
- (id)initWithStyle:(UITableViewStyle)style | |
{ | |
self = [super initWithStyle:style]; | |
if (self) { | |
// Custom initialization | |
} | |
return self; | |
} | |
- (void)loadRecordsFromCoreData { | |
[self.managedObjectContext performBlockAndWait:^{ | |
[self.managedObjectContext reset]; | |
NSError *error = nil; | |
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:self.entityName]; | |
[request setSortDescriptors:[NSArray arrayWithObject: | |
[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES]]]; | |
self.dates = [self.managedObjectContext executeFetchRequest:request error:&error]; | |
}]; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
self.managedObjectContext = [[SDCoreDataController sharedInstance] newManagedObjectContext]; | |
self.dateFormatter = [[NSDateFormatter alloc] init]; | |
[self.dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; | |
[self.dateFormatter setDateStyle:NSDateFormatterMediumStyle]; | |
self.entityName = @"Holiday"; | |
[self loadRecordsFromCoreData]; | |
//UISearch | |
// Initialize the filteredCandyArray with a capacity equal to the candyArray's capacity | |
self.filteredResultsArray = [NSMutableArray arrayWithCapacity:[self.dates count]]; | |
} | |
- (void)viewDidAppear:(BOOL)animated { | |
[super viewDidAppear:animated]; | |
[self checkSyncStatus]; | |
[[NSNotificationCenter defaultCenter] addObserverForName:@"SDSyncEngineSyncCompleted" object:nil queue:nil usingBlock:^(NSNotification *note) { | |
[self loadRecordsFromCoreData]; | |
[self.tableView reloadData]; | |
}]; | |
[[SDSyncEngine sharedEngine] addObserver:self forKeyPath:@"syncInProgress" options:NSKeyValueObservingOptionNew context:nil]; | |
} | |
- (void)viewDidDisappear:(BOOL)animated { | |
[super viewDidDisappear:animated]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"SDSyncEngineSyncCompleted" object:nil]; | |
[[SDSyncEngine sharedEngine] removeObserver:self forKeyPath:@"syncInProgress"]; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
return (interfaceOrientation == UIInterfaceOrientationPortrait); | |
} | |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { | |
return YES; | |
} | |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { | |
if (editingStyle == UITableViewCellEditingStyleDelete) { | |
NSManagedObject *date = [self.dates objectAtIndex:indexPath.row]; | |
[self.managedObjectContext performBlockAndWait:^{ | |
[self.managedObjectContext deleteObject:date]; | |
NSError *error = nil; | |
BOOL saved = [self.managedObjectContext save:&error]; | |
if (!saved) { | |
NSLog(@"Error saving main context: %@", error); | |
} | |
[[SDCoreDataController sharedInstance] saveMasterContext]; | |
[self loadRecordsFromCoreData]; | |
[self.tableView reloadData]; | |
}]; | |
} | |
} | |
#pragma mark - Table view data source | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
// Return the number of sections. | |
return 1; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
// Return the number of rows in the section. | |
if (tableView == self.searchDisplayController.searchResultsTableView) { | |
return [filteredResultsArray count]; | |
} else { | |
return [self.dates count]; | |
} | |
//return [self.dates count]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
TableViewCell *cell = nil; | |
// Check to see whether the normal table or search results table is being displayed and set the Candy object from the appropriate array | |
//if/else #B | |
if (tableView == self.searchDisplayController.searchResultsTableView) { | |
static NSString *CellIdentifier = @"HolidayCell"; | |
cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
Holiday *holiday = [filteredResultsArray objectAtIndex:indexPath.row]; | |
NSLog(@"the holiday is %@", holiday.name); | |
cell.nameLabel.text = holiday.name; | |
} else { | |
static NSString *CellIdentifier = @"HolidayCell"; | |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
Holiday *holiday = [self.dates objectAtIndex:indexPath.row]; | |
cell.nameLabel.text = holiday.name; | |
cell.dateLabel.text = holiday.latitude; | |
if (holiday.image != nil) { | |
UIImage *image = [UIImage imageWithData:holiday.image]; | |
cell.imageView.image = image; | |
} else { | |
cell.imageView.image = nil; | |
} | |
}// if/else #B | |
return cell; | |
} | |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { | |
} | |
- (void)viewDidUnload { | |
[self setRefreshButton:nil]; | |
[super viewDidUnload]; | |
} | |
- (IBAction)refreshButtonTouched:(id)sender { | |
[[SDSyncEngine sharedEngine] startSync]; | |
} | |
- (void)checkSyncStatus { | |
if ([[SDSyncEngine sharedEngine] syncInProgress]) { | |
[self replaceRefreshButtonWithActivityIndicator]; | |
} else { | |
[self removeActivityIndicatorFromRefreshButon]; | |
} | |
} | |
- (void)replaceRefreshButtonWithActivityIndicator { | |
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; | |
[activityIndicator setAutoresizingMask:(UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin)]; | |
[activityIndicator startAnimating]; | |
UIBarButtonItem *activityItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator]; | |
self.navigationItem.leftBarButtonItem = activityItem; | |
} | |
- (void)removeActivityIndicatorFromRefreshButon { | |
self.navigationItem.leftBarButtonItem = self.refreshButton; | |
} | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context | |
{ | |
if ([keyPath isEqualToString:@"syncInProgress"]) { | |
[self checkSyncStatus]; | |
} | |
} | |
#pragma mark Content Filtering | |
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { | |
// Update the filtered array based on the search text and scope. | |
// Remove all objects from the filtered search array | |
[self.filteredResultsArray removeAllObjects]; | |
// Filter the array using NSPredicate | |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchText]; | |
filteredResultsArray = [NSMutableArray arrayWithArray:[self.dates filteredArrayUsingPredicate:predicate]]; | |
} | |
#pragma mark - UISearchDisplayController Delegate Methods | |
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { | |
// Tells the table data source to reload when text changes | |
[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 { | |
// Tells the table data source to reload when scope bar selection changes | |
[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; | |
} | |
#pragma mark - Table view delegate | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
// Navigation logic may go here. Create and push another view controller. | |
/* | |
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; | |
// ... | |
// Pass the selected object to the new view controller. | |
[self.navigationController pushViewController:detailViewController animated:YES]; | |
[detailViewController release]; | |
*/ | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment