From eaa3d1443eedc1e0633db6db997857b50e91ef75 Mon Sep 17 00:00:00 2001 From: palaniraja Date: Wed, 28 Mar 2012 23:13:35 +0530 Subject: [PATCH] Basic implementation for country picker with UITableview completed. --- CountryPicker/CountryPickerTableView.h | 42 ++++ CountryPicker/CountryPickerTableView.m | 180 ++++++++++++++++++ .../project.pbxproj | 6 + Examples/CountryPickerDemo/ViewController.h | 1 + Examples/CountryPickerDemo/ViewController.m | 6 + .../en.lproj/ViewController.xib | 68 +++++-- 6 files changed, 290 insertions(+), 13 deletions(-) create mode 100644 CountryPicker/CountryPickerTableView.h create mode 100644 CountryPicker/CountryPickerTableView.m diff --git a/CountryPicker/CountryPickerTableView.h b/CountryPicker/CountryPickerTableView.h new file mode 100644 index 00000000..d97fa13a --- /dev/null +++ b/CountryPicker/CountryPickerTableView.h @@ -0,0 +1,42 @@ +// +// CountryPickerTableView.h +// CountryPickerDemo +// +// Created by Palaniraja on 28/03/12. +// Copyright (c) 2012 __MyCompanyName__. All rights reserved. +// + +#import +#import "CountryPicker.h" + + +@protocol CountryPickerTableViewDelegate + +- (void)countryPickerTableView:(UITableView *)pickerTableView didSelectCountryWithName:(NSString *)name code:(NSString *)code; + +@end + +@interface CountryPickerTableView : UITableView { + NSIndexPath *selectedIndexPath; +} + + + ++ (NSArray *)countryNames; ++ (NSArray *)countryCodes; ++ (NSDictionary *)countryNamesByCode; ++ (NSDictionary *)countryCodesByName; + +@property (nonatomic, AH_WEAK) id countrySelectionDelegate; +@property (nonatomic, copy) NSString *selectedCountryName; +@property (nonatomic, copy) NSString *selectedCountryCode; + +- (void)setWithLocale:(NSLocale *)locale; + +@end + + + + + + diff --git a/CountryPicker/CountryPickerTableView.m b/CountryPicker/CountryPickerTableView.m new file mode 100644 index 00000000..291ab962 --- /dev/null +++ b/CountryPicker/CountryPickerTableView.m @@ -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 () +// +// +// +//@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)dataSource +{ + //does nothing +} +- (void)setDelegate:(id)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 diff --git a/Examples/CountryPickerDemo.xcodeproj/project.pbxproj b/Examples/CountryPickerDemo.xcodeproj/project.pbxproj index 7beee4ba..b0d129e5 100644 --- a/Examples/CountryPickerDemo.xcodeproj/project.pbxproj +++ b/Examples/CountryPickerDemo.xcodeproj/project.pbxproj @@ -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 */ @@ -534,6 +535,8 @@ 01D3A0D114E610F5002FC9B1 /* ZA.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ZA.png; sourceTree = ""; }; 01D3A0D214E610F5002FC9B1 /* ZM.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ZM.png; sourceTree = ""; }; 01D3A0D314E610F5002FC9B1 /* ZW.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = ZW.png; sourceTree = ""; }; + AE14E5541523846C001811F8 /* CountryPickerTableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CountryPickerTableView.h; sourceTree = ""; }; + AE14E5551523846C001811F8 /* CountryPickerTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CountryPickerTableView.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -609,6 +612,8 @@ 01D39FD414E60FE1002FC9B1 /* Countries.plist */, 01D39FD514E60FE1002FC9B1 /* CountryPicker.h */, 01D39FD614E60FE1002FC9B1 /* CountryPicker.m */, + AE14E5541523846C001811F8 /* CountryPickerTableView.h */, + AE14E5551523846C001811F8 /* CountryPickerTableView.m */, ); name = CountryPicker; path = ../CountryPicker; @@ -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; }; diff --git a/Examples/CountryPickerDemo/ViewController.h b/Examples/CountryPickerDemo/ViewController.h index fbe754bb..c59b9f4b 100644 --- a/Examples/CountryPickerDemo/ViewController.h +++ b/Examples/CountryPickerDemo/ViewController.h @@ -8,6 +8,7 @@ #import #import "CountryPicker.h" +#import "CountryPickerTableView.h" @interface ViewController : UIViewController diff --git a/Examples/CountryPickerDemo/ViewController.m b/Examples/CountryPickerDemo/ViewController.m index 1b452907..105b2161 100644 --- a/Examples/CountryPickerDemo/ViewController.m +++ b/Examples/CountryPickerDemo/ViewController.m @@ -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 diff --git a/Examples/CountryPickerDemo/en.lproj/ViewController.xib b/Examples/CountryPickerDemo/en.lproj/ViewController.xib index 87efdfb5..8b0b9852 100644 --- a/Examples/CountryPickerDemo/en.lproj/ViewController.xib +++ b/Examples/CountryPickerDemo/en.lproj/ViewController.xib @@ -2,16 +2,17 @@ 1280 - 11C74 + 10K549 1938 - 1138.23 - 567.00 + 1038.36 + 461.00 com.apple.InterfaceBuilder.IBCocoaTouchPlugin 933 IBUIPickerView + IBUITableView IBUIView IBUILabel IBProxyObject @@ -42,19 +43,16 @@ {{0, 244}, {320, 216}} - - _NS:650 IBCocoaTouchFramework YES 292 - {{61, 50}, {198, 21}} + {{6, 9}, {198, 21}} - _NS:328 NO YES 7 @@ -82,11 +80,10 @@ 292 - {{136, 105}, {48, 21}} + {{237, 9}, {48, 21}} - - _NS:328 + NO YES 7 @@ -101,6 +98,27 @@ + + + 274 + {{13, 38}, {295, 198}} + + + + + 3 + MQA + + YES + IBCocoaTouchFramework + YES + 1 + 0 + YES + 44 + 22 + 22 + {{0, 20}, {320, 460}} @@ -150,7 +168,15 @@ - 9 + 17 + + + + delegate + + + + 21 @@ -177,8 +203,9 @@ - + + @@ -197,6 +224,11 @@ + + 15 + + + @@ -206,6 +238,8 @@ com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin + CountryPickerTableView + com.apple.InterfaceBuilder.IBCocoaTouchPlugin com.apple.InterfaceBuilder.IBCocoaTouchPlugin CountryPicker com.apple.InterfaceBuilder.IBCocoaTouchPlugin @@ -214,7 +248,7 @@ - 14 + 21 @@ -226,6 +260,14 @@ ./Classes/CountryPicker.h + + CountryPickerTableView + UITableView + + IBProjectSource + ./Classes/CountryPickerTableView.h + + ViewController UIViewController