前端之家收集整理的这篇文章主要介绍了
PickView简单的依赖,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#import "YXDependentComponentPickerViewController.h"
#define kStateComponent 0
#define kZipComponent 1
@interface YXDependentComponentPickerViewController () <UIPickerViewDataSource,UIPickerViewDelegate>
@property (weak,nonatomic) IBOutlet UIPickerView *DependentPicker;
- (IBAction)buttonPress:(UIButton *)sender;
/** <#type#>*/
@property (nonatomic,strong) NSDictionary *stateZip; // dict
/** <#type#>*/
@property (nonatomic,strong) NSArray *states; // keys
/** <#type#>*/
@property (nonatomic,strong) NSArray *zips; // values
// 字典中包含n个数组
// stateZip allKeys - > states
// stateZip key -> value : states
@end
@implementation YXDependentComponentPickerViewController
- (NSArray *)states {
if (_states == nil) {
NSMutableArray *keyTmp = [[NSMutableArray alloc] init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
for (int i = 0; i < 26; i++) {
char ch = 'A' + i;
NSString *key = [NSString stringWithFormat:@"%c",ch];
[keyTmp addObject:key];
NSMutableArray *tmpArr = [[NSMutableArray alloc] init];
for (int j = 0; j < 10; j++) {
[tmpArr addObject:[NSString stringWithFormat:@"%@---%d",key,j]];
}
[dict setObject:tmpArr forKey:key];
}
self.stateZip = dict;
self.states = keyTmp;
}
return _states;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSString *selectedState = self.states[0]; // 字典中第1个key -- A
self.zips = self.stateZip[selectedState];
}
- (IBAction)buttonPress:(UIButton *)sender {
// 根据位置获取数据
NSInteger stateRow = [self.DependentPicker selectedRowInComponent:kStateComponent];
NSInteger zipRow = [self.DependentPicker selectedRowInComponent:kZipComponent];
NSString *state = self.states[stateRow];
NSString *zip = self.zips[zipRow];
NSString *title = [[NSString alloc] initWithFormat:@"You selected zip code %@",zip];
NSString *message = [[NSString alloc] initWithFormat:@"%@ is in %@",zip,state];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];
[alertView show];
}
#pragma mark -
#pragma mark picker data source methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == kStateComponent) {
return self.states.count;
}
return self.zips.count;
}
#pragma mark picker delegate methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == kStateComponent) {
return [self.states objectAtIndex:row];
} return self.zips[row];
}
/**
* 只要选择器的选择发生了变化,就会调用这个方法,在这个方法中检查component参数可以知道发生变化的是否是左侧的滚轮,如果确实是左侧的滚轮发生了变化,那么右侧的内容也会跟着发生变化。
*
*/
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == kStateComponent) {
NSString *selectedState = self.states[row];
self.zips = self.stateZip[selectedState];
[self.DependentPicker reloadComponent:kZipComponent];
// [self.DependentPicker reloadAllComponents];
[self.DependentPicker selectRow:0 inComponent:kZipComponent animated:YES];
}
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (component == kZipComponent) {
return 90.0f;
}
return 200.f;
}
@end