Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions CountryPicker/CountryPickerTableView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// CountryPickerTableView.h
// CountryPickerDemo
//
// Created by Palaniraja on 28/03/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "CountryPicker.h"


@protocol CountryPickerTableViewDelegate

- (void)countryPickerTableView:(UITableView *)pickerTableView didSelectCountryWithName:(NSString *)name code:(NSString *)code;

@end

@interface CountryPickerTableView : UITableView <UITableViewDelegate, UITableViewDataSource>{
NSIndexPath *selectedIndexPath;
}



+ (NSArray *)countryNames;
+ (NSArray *)countryCodes;
+ (NSDictionary *)countryNamesByCode;
+ (NSDictionary *)countryCodesByName;

@property (nonatomic, AH_WEAK) id<CountryPickerTableViewDelegate> countrySelectionDelegate;
@property (nonatomic, copy) NSString *selectedCountryName;
@property (nonatomic, copy) NSString *selectedCountryCode;

- (void)setWithLocale:(NSLocale *)locale;

@end






180 changes: 180 additions & 0 deletions CountryPicker/CountryPickerTableView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
//
// CountryPickerTableView.m
// CountryPickerDemo
//
// Created by Palaniraja on 28/03/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CountryPickerTableView.h"

//@interface CountryPickerTableView () <UITableViewDelegate, UITableViewDataSource>
//
//
//
//@end


@implementation CountryPickerTableView

static NSArray *countryNames = nil;
static NSArray *countryCodes = nil;
static NSDictionary *countryNamesByCode = nil;
static NSDictionary *countryCodesByName = nil;


@synthesize countrySelectionDelegate;

#pragma mark copied from CountryPicker
+ (void)initialize
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Countries" ofType:@"plist"];
countryNamesByCode = [[NSDictionary alloc] initWithContentsOfFile:path];

NSMutableDictionary *codesByName = [NSMutableDictionary dictionary];
for (NSString *code in [countryNamesByCode allKeys])
{
[codesByName setObject:code forKey:[countryNamesByCode objectForKey:code]];
}
countryCodesByName = [codesByName copy];

NSArray *names = [countryNamesByCode allValues];
countryNames = AH_RETAIN([names sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]);

NSMutableArray *codes = [NSMutableArray arrayWithCapacity:[names count]];
for (NSString *name in countryNames)
{
[codes addObject:[countryCodesByName objectForKey:name]];
}
countryCodes = [codes copy];
}

+ (NSArray *)countryNames
{
return countryNames;
}

+ (NSArray *)countryCodes
{
return countryCodes;
}

+ (NSDictionary *)countryNamesByCode
{
return countryNamesByCode;
}

+ (NSDictionary *)countryCodesByName
{
return countryCodesByName;
}

- (void)setup
{
[super setDataSource:self];
[super setDelegate:self];
}

- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
[self setup];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
[self setup];
}
return self;
}

- (void)setWithLocale:(NSLocale *)locale
{
self.selectedCountryCode = [locale objectForKey:NSLocaleCountryCode];
}

- (void)setDataSource:(id<UITableViewDataSource>)dataSource
{
//does nothing
}
- (void)setDelegate:(id<UITableViewDelegate>)newdelegate
{
//does nothing
countrySelectionDelegate = newdelegate;
}


- (void)setSelectedCountryCode:(NSString *)countryCode
{
NSInteger index = [countryCodes indexOfObject:countryCode];
if (index != NSNotFound)
{
// [self selectRow:index inComponent:0 animated:NO];
[self selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
}

- (NSString *)selectedCountryCode
{
NSInteger index = selectedIndexPath.row;
return [countryCodes objectAtIndex:index];
}

- (void)setSelectedCountryName:(NSString *)countryName
{
NSInteger index = [countryNames indexOfObject:countryName];
if (index != NSNotFound)
{
[self selectRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
}

- (NSString *)selectedCountryName
{
NSInteger index = selectedIndexPath.row;
return [countryNames objectAtIndex:index];
}


#pragma mark Datasource & Delegates

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [countryCodes count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"countrypickercell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = AH_AUTORELEASE([[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] );
cell.textLabel.font = [UIFont systemFontOfSize:15.0f];
}

cell.textLabel.text = [countryNames objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[[countryCodes objectAtIndex:indexPath.row] stringByAppendingPathExtension:@"png"]];

return cell;


}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndexPath = indexPath;
// NSLog(@"selected Indexpath: %@", indexPath);

// NSLog(@"Selected country name: %@ code: %@", self.selectedCountryName, self.selectedCountryCode);
[countrySelectionDelegate countryPickerTableView:self didSelectCountryWithName:self.selectedCountryName code:self.selectedCountryCode];
}

@end
6 changes: 6 additions & 0 deletions Examples/CountryPickerDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
01D3A1CA14E610F6002FC9B1 /* ZA.png in Resources */ = {isa = PBXBuildFile; fileRef = 01D3A0D114E610F5002FC9B1 /* ZA.png */; };
01D3A1CB14E610F6002FC9B1 /* ZM.png in Resources */ = {isa = PBXBuildFile; fileRef = 01D3A0D214E610F5002FC9B1 /* ZM.png */; };
01D3A1CC14E610F6002FC9B1 /* ZW.png in Resources */ = {isa = PBXBuildFile; fileRef = 01D3A0D314E610F5002FC9B1 /* ZW.png */; };
AE14E5561523846C001811F8 /* CountryPickerTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = AE14E5551523846C001811F8 /* CountryPickerTableView.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -534,6 +535,8 @@
01D3A0D114E610F5002FC9B1 /* ZA.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ZA.png; sourceTree = "<group>"; };
01D3A0D214E610F5002FC9B1 /* ZM.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ZM.png; sourceTree = "<group>"; };
01D3A0D314E610F5002FC9B1 /* ZW.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ZW.png; sourceTree = "<group>"; };
AE14E5541523846C001811F8 /* CountryPickerTableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CountryPickerTableView.h; sourceTree = "<group>"; };
AE14E5551523846C001811F8 /* CountryPickerTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CountryPickerTableView.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -609,6 +612,8 @@
01D39FD414E60FE1002FC9B1 /* Countries.plist */,
01D39FD514E60FE1002FC9B1 /* CountryPicker.h */,
01D39FD614E60FE1002FC9B1 /* CountryPicker.m */,
AE14E5541523846C001811F8 /* CountryPickerTableView.h */,
AE14E5551523846C001811F8 /* CountryPickerTableView.m */,
);
name = CountryPicker;
path = ../CountryPicker;
Expand Down Expand Up @@ -1187,6 +1192,7 @@
01D39FC314E60FBC002FC9B1 /* AppDelegate.m in Sources */,
01D39FC614E60FBC002FC9B1 /* ViewController.m in Sources */,
01D39FD814E60FE1002FC9B1 /* CountryPicker.m in Sources */,
AE14E5561523846C001811F8 /* CountryPickerTableView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
1 change: 1 addition & 0 deletions Examples/CountryPickerDemo/ViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import <UIKit/UIKit.h>
#import "CountryPicker.h"
#import "CountryPickerTableView.h"

@interface ViewController : UIViewController <CountryPickerDelegate>

Expand Down
6 changes: 6 additions & 0 deletions Examples/CountryPickerDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ - (void)countryPicker:(CountryPicker *)picker didSelectCountryWithName:(NSString
codeLabel.text = code;
}


- (void)countryPickerTableView:(UITableView *)pickerTableView didSelectCountryWithName:(NSString *)name code:(NSString *)code{

nameLabel.text = name;
codeLabel.text = code;
}
@end
Loading