In the latest IOS project, the selector is used in several places. For example, the commonly used regional selector (province, city, and district) has implemented a custom Dialog on the android platform. However, I am not very familiar with IOS. I downloaded some examples from the Internet, mostly using PickerView, and then the provinces and cities are divided into three columns, for example:
This effect is also good, but sometimes the zone name is too long to see, and there is no button to point. You cannot enter the address yourself.
You can enter:
Let's look at the source code.
First header file:
[Cpp]
//
// NerveAreaSelectorViewController. h
//
// Provincial/municipal level 3 selector, which can be input by yourself
//
// Created by integrated video card on 13-5-17.
// Copyright (c) 2013 integrated graphics card zxingming@qq.com. All rights reserved.
//
# Import <UIKit/UIKit. h>
Typedef enum {
PROVINCE,
CITY,
AREA
} NerveSelectoreType;
//
// Selector protocol, which can be implemented in the called 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; // the current selection type, province, city, and district.
Id <NerveAreaSelectDelegate> delegate;
}
@ Property (weak, nonatomic) IBOutlet UITextField * areaTX;
@ Property (weak, nonatomic) IBOutlet UITableView * areaTableView;
//
// Initialize data
//
-(Id) initWithDelegate :( id <NerveAreaSelectDelegate>) targetDelegate;
//
// Click the OK button
//
-(IBAction) onOkBtnClick :( id) sender;
//
// Cancel button clicking event
//
-(IBAction) okCannelBtnClick :( id) sender;
//
// Hide the keyboard
//
-(IBAction) exitInput :( id) sender;
@ End
//
// NerveAreaSelectorViewController. h
//
// Provincial/municipal level 3 selector, which can be input by yourself
//
// Created by integrated video card on 13-5-17.
// Copyright (c) 2013 integrated graphics card zxingming@qq.com. All rights reserved.
//
# Import <UIKit/UIKit. h>
Typedef enum {
PROVINCE,
CITY,
AREA
} NerveSelectoreType;
//
// Selector protocol, which can be implemented in the called 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; // the current selection type, province, city, and district.
Id <NerveAreaSelectDelegate> delegate;
}
@ Property (weak, nonatomic) IBOutlet UITextField * areaTX;
@ Property (weak, nonatomic) IBOutlet UITableView * areaTableView;
//
// Initialize data
//
-(Id) initWithDelegate :( id <NerveAreaSelectDelegate>) targetDelegate;
//
// Click the OK button
//
-(IBAction) onOkBtnClick :( id) sender;
//
// Cancel button clicking event
//
-(IBAction) okCannelBtnClick :( id) sender;
//
// Hide the keyboard
//
-(IBAction) exitInput :( id) sender;
@ End
Then there is a specific implementation class. Here we will mainly discuss some main methods.
Data Reading method:
[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;
}
How to display 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: mcmcellidentifier];
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: mcmcellidentifier];
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;
}
When you select a cell:
[Cpp]
// The table is selected.
-(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];
}
}
// The table is selected.
-(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];
}
}
Click OK:
[Cpp]
-(IBAction) onOkBtnClick :( id) sender {
NSLog (@ "OK click ");
[Delegate onAreaSelect: areaTX. text];
}
-(IBAction) onOkBtnClick :( id) sender {
NSLog (@ "OK click ");
[Delegate onAreaSelect: areaTX. text];
}