最近做的IOS項目中,要用到好幾處地方要用到選擇器。比如說很常用的地區選擇器(省市區),在android平台下已經實現了自定義一個Dialog。但是對IOS並不太熟悉,去網上下載了一些例子,大多是用PickerView,然後省市區分三列,如:
這樣的效果也不錯,只是有些時候區名太長,就看不到了,而且沒有按鈕可以點。還有一個就是不能自己輸入地址。
用戶可以自己輸入:
看看源代碼吧。
先是頭文件:
[cpp]
//
// NerveAreaSelectorViewController.h
//
// 省市區三級選擇器,可以自己輸入
//
// Created by 集成顯卡 on 13-5-17.
// Copyright (c) 2013年 集成顯卡 [email protected] . All rights reserved.
//
#import <UIKit/UIKit.h>
typedef enum {
PROVINCE,
CITY,
AREA
} NerveSelectoreType;
//
//選擇器協議,在調用的ViewController中實現此協議即可
//
@protocol NerveAreaSelectDelegate <NSObject>
@optional
- (void)onAreaSelect:(NSString *)selectValue;
- (void)onAreaCannel;
@end
@interface NerveAreaSelectorViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
NSArray *provinces, *cities, *areas;
NSString *province, *city, *area;
NSInteger selectType;//當前的選擇類型,省,市,區
id<NerveAreaSelectDelegate> delegate;
}
@property (weak, nonatomic) IBOutlet UITextField *areaTX;
@property (weak, nonatomic) IBOutlet UITableView *areaTableView;
//
//初始化數據
//
-(id) initWithDelegate:(id<NerveAreaSelectDelegate>) targetDelegate;
//
//確定按鈕點擊事件
//
- (IBAction)onOkBtnClick:(id)sender;
//
//取消按鈕點擊事件
//
- (IBAction)okCannelBtnClick:(id)sender;
//
//隱藏鍵盤用的
//
- (IBAction)exitInput:(id)sender;
@end
//
// NerveAreaSelectorViewController.h
//
// 省市區三級選擇器,可以自己輸入
//
// Created by 集成顯卡 on 13-5-17.
// Copyright (c) 2013年 集成顯卡 [email protected] . All rights reserved.
//
#import <UIKit/UIKit.h>
typedef enum {
PROVINCE,
CITY,
AREA
} NerveSelectoreType;
//
//選擇器協議,在調用的ViewController中實現此協議即可
//
@protocol NerveAreaSelectDelegate <NSObject>
@optional
- (void)onAreaSelect:(NSString *)selectValue;
- (void)onAreaCannel;
@end
@interface NerveAreaSelectorViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
NSArray *provinces, *cities, *areas;
NSString *province, *city, *area;
NSInteger selectType;//當前的選擇類型,省,市,區
id<NerveAreaSelectDelegate> delegate;
}
@property (weak, nonatomic) IBOutlet UITextField *areaTX;
@property (weak, nonatomic) IBOutlet UITableView *areaTableView;
//
//初始化數據
//
-(id) initWithDelegate:(id<NerveAreaSelectDelegate>) targetDelegate;
//
//確定按鈕點擊事件
//
- (IBAction)onOkBtnClick:(id)sender;
//
//取消按鈕點擊事件
//
- (IBAction)okCannelBtnClick:(id)sender;
//
//隱藏鍵盤用的
//
- (IBAction)exitInput:(id)sender;
@end
然後是具體實現類,這裡主要講一些主要的方法。
讀取數據方法:
[cpp]
-(id)initWithDelegate:(id<NerveAreaSelectDelegate>)targetDelegate{
delegate = targetDelegate;
[self readData];
selectType = PROVINCE;
self.navigationController.navigationBarHidden = NO;
self.title = @"ssss";
UIBarButtonItem* leftB = [[UIBarButtonItem alloc] init];
leftB.title = @"ss";
self.navigationItem.backBarButtonItem = leftB;
return self;
}
-(id)initWithDelegate:(id<NerveAreaSelectDelegate>)targetDelegate{
delegate = targetDelegate;
[self readData];
selectType = PROVINCE;
self.navigationController.navigationBarHidden = NO;
self.title = @"ssss";
UIBarButtonItem* leftB = [[UIBarButtonItem alloc] init];
leftB.title = @"ss";
self.navigationItem.backBarButtonItem = leftB;
return self;
}
顯示tableCell的方法:
[cpp]
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
int index = [indexPath row];
//int section = [indexPath section];
static NSString* CustomCellIdentifier = @"NerveAreaSelectorCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellIdentifier];
}
switch (selectType) {
case PROVINCE:
cell.textLabel.text = [[provinces objectAtIndex:index]
objectForKey:@"state"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
case CITY:
cell.textLabel.text = [[cities objectAtIndex:index]
objectForKey:@"city"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
default:
cell.textLabel.text = [areas objectAtIndex:index];
cell.accessoryType = UITableViewCellAccessoryNone;
break;
}
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
int index = [indexPath row];
//int section = [indexPath section];
static NSString* CustomCellIdentifier = @"NerveAreaSelectorCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellIdentifier];
}
switch (selectType) {
case PROVINCE:
cell.textLabel.text = [[provinces objectAtIndex:index]
objectForKey:@"state"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
case CITY:
cell.textLabel.text = [[cities objectAtIndex:index]
objectForKey:@"city"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
break;
default:
cell.textLabel.text = [areas objectAtIndex:index];
cell.accessoryType = UITableViewCellAccessoryNone;
break;
}
return cell;
}
用戶選擇單元格時:
[cpp]
//表格被選擇
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int selectIndex = [indexPath row];
int cellCount = 0;
switch (selectType) {
case PROVINCE:
province = [[provinces objectAtIndex:selectIndex] objectForKey:@"state"];
cities = [[provinces objectAtIndex:selectIndex] objectForKey:@"cities"];
selectType = CITY;
cellCount = [cities count];
break;
case CITY:
city = [[cities objectAtIndex:selectIndex] objectForKey:@"city"];
areas = [[cities objectAtIndex:selectIndex] objectForKey:@"areas"];
selectType = AREA;
cellCount = [areas count];
break;
default:
area = [areas objectAtIndex:selectIndex];
break;
}
NSString* areaValue = [NSString stringWithFormat:@"%@ %@ %@",province, city,area];
NSLog(@"select=%@", areaValue);
self.areaTX.text = areaValue;
[areaTableView reloadData];
if(cellCount > 0){
NSIndexPath* topIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[areaTableView scrollToRowAtIndexPath:topIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
//表格被選擇
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
int selectIndex = [indexPath row];
int cellCount = 0;
switch (selectType) {
case PROVINCE:
province = [[provinces objectAtIndex:selectIndex] objectForKey:@"state"];
cities = [[provinces objectAtIndex:selectIndex] objectForKey:@"cities"];
selectType = CITY;
cellCount = [cities count];
break;
case CITY:
city = [[cities objectAtIndex:selectIndex] objectForKey:@"city"];
areas = [[cities objectAtIndex:selectIndex] objectForKey:@"areas"];
selectType = AREA;
cellCount = [areas count];
break;
default:
area = [areas objectAtIndex:selectIndex];
break;
}
NSString* areaValue = [NSString stringWithFormat:@"%@ %@ %@",province, city,area];
NSLog(@"select=%@", areaValue);
self.areaTX.text = areaValue;
[areaTableView reloadData];
if(cellCount > 0){
NSIndexPath* topIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[areaTableView scrollToRowAtIndexPath:topIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
}
確定按鈕被點擊:
[cpp]
- (IBAction)onOkBtnClick:(id)sender {
NSLog(@"OK click");
[delegate onAreaSelect:areaTX.text];
}
- (IBAction)onOkBtnClick:(id)sender {
NSLog(@"OK click");
[delegate onAreaSelect:areaTX.text];
}