Skip to content

Instantly share code, notes, and snippets.

@zackbraksa
Created June 14, 2014 23:10
Show Gist options
  • Save zackbraksa/3bd744f08d81bcc7cd05 to your computer and use it in GitHub Desktop.
Save zackbraksa/3bd744f08d81bcc7cd05 to your computer and use it in GitHub Desktop.
//
// PickCityViewController.m
//
// Created by zakaria on 12/28/13.
// Copyright (c) 2013 Zakaria Braksa. All rights reserved.
//
#import "PickCityViewController.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>
#import <SSKeychain.h>
#import "constants.h"
#import "ValidateCityViewController.h"
@interface PickCityViewController ()
@end
@implementation PickCityViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSStringEncoding encoding;
NSString* content;
NSString* path = [[NSBundle mainBundle] pathForResource:@"ios_cities" ofType:@"txt"];
if(path)
{
content = [NSString stringWithContentsOfFile:path usedEncoding:&encoding error:NULL];
}
if (content)
{
NSLog(@" content of file is %@",content);
}
NSCharacterSet *newlineCharSet = [NSCharacterSet newlineCharacterSet];
// NSString* fileContents = [NSString stringWithContentsOfFile:@"ios_cities.txt"
// encoding:NSUTF8StringEncoding
// error:nil];
cities = [content componentsSeparatedByCharactersInSet:newlineCharSet];
[self.tableView reloadData];
filteredCitiesArray = [NSMutableArray arrayWithCapacity:[cities count]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
//
// if(cell == nil) {
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
// }
//
// cell.textLabel.text = [cities objectAtIndex:indexPath.row];
//
// return cell;
//}
#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
[filteredCitiesArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchText];
filteredCitiesArray = [NSMutableArray arrayWithArray:[cities filteredArrayUsingPredicate:predicate]];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [filteredCitiesArray count];
}
else
{
return [cities count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cityCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text = [filteredCitiesArray objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = cities[indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)theTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* rowSelected;
if([filteredCitiesArray count] != 0){
rowSelected = [filteredCitiesArray objectAtIndex:indexPath.row];
}else{
rowSelected = [cities objectAtIndex:indexPath.row];
}
ValidateCityViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ValidateCity"];
viewController.choice = rowSelected;
[self.navigationController pushViewController:viewController animated:YES];
}
- (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