IOS下省市区选择器实现[基于TableView]
最近做的IOS项目中,要用到好几处地方要用到选择器。比如说很常用的地区选择器(省市区),在android平台下已经实现了自定义一个Dialog。但是对IOS并不太熟悉,去网上下载了一些例子,大多是用PickerView,然后省市区分三列,如:
这样的效果也不错,只是有些时候区名太长,就看不到了,而且没有按钮可以点。还有一个就是不能自己输入地址。
用户可以自己输入:
看看源代码吧。
先是头文件:
[cpp]
//
// NerveAreaSelectorViewController.h
//
// 省市区易做图选择器,可以自己输入
//
// Created by 集成显卡 on 13-5-17.
// Copyright (c) 2013年 集成显卡 zxingming@qq.com . 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
@inte易做图ce 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年 集成显卡 zxingming@qq.com . 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
@inte易做图ce 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]
补充:移动开发 , IOS ,