首页 > 代码库 > 城市选择功能实现
城市选择功能实现
步骤一:解析plist文件,创建对应的模型。
+ (instancetype)cityWithDict:(NSDictionary *)dict
{
return [[selfalloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [superinit];
if (self) {
[selfsetValuesForKeysWithDictionary:dict];
}
return self;
}
步骤二:用一个数组将模型保存起来。
+ (NSArray *)cities
{
NSArray *arrayC = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities.plist"ofType:nil]];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dictin arrayC) {
SUNCityInfo *cityInfo = [selfcityWithDict:dict];
[arrayM addObject:cityInfo];
}
return arrayM;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@,%p>{name:%@,cities:%@}",self.class,self,self.name,self.cities];
}
步骤三:手动代码创建UIPickerView,实现它的数据源和代理方法。
#pragma mark -数据源
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component ==0) {
return self.cities.count;
}else{
int index = [self.picker selectedRowInComponent:0];
SUNCityInfo *cityInfo = self.cities[index];
return cityInfo.cities.count;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component ==0) {
SUNCityInfo *cityInfo = self.cities[row];
return cityInfo.name;
}else{
int index = [self.picker selectedRowInComponent:0];
SUNCityInfo *cityInfo = self.cities[index];
return cityInfo.cities[row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component ==0) {
[pickerView reloadComponent:1];
[pickerView selectRow:0inComponent:1animated:YES];
}
// 获得选中的省份名称
int index = [self.picker selectedRowInComponent:0];
SUNCityInfo *cityInfo = self.cities[index];
int cIndex = [self.picker selectedRowInComponent:1];
self.cityLabel.text = [NSString stringWithFormat:@"%@ %@",cityInfo.name,cityInfo.cities[cIndex]] ;
}
+ (instancetype)cityWithDict:(NSDictionary *)dict
{
return [[selfalloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [superinit];
if (self) {
[selfsetValuesForKeysWithDictionary:dict];
}
return self;
}
步骤二:用一个数组将模型保存起来。
+ (NSArray *)cities
{
NSArray *arrayC = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities.plist"ofType:nil]];
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dictin arrayC) {
SUNCityInfo *cityInfo = [selfcityWithDict:dict];
[arrayM addObject:cityInfo];
}
return arrayM;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@,%p>{name:%@,cities:%@}",self.class,self,self.name,self.cities];
}
步骤三:手动代码创建UIPickerView,实现它的数据源和代理方法。
#pragma mark -数据源
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component ==0) {
return self.cities.count;
}else{
int index = [self.picker selectedRowInComponent:0];
SUNCityInfo *cityInfo = self.cities[index];
return cityInfo.cities.count;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component ==0) {
SUNCityInfo *cityInfo = self.cities[row];
return cityInfo.name;
}else{
int index = [self.picker selectedRowInComponent:0];
SUNCityInfo *cityInfo = self.cities[index];
return cityInfo.cities[row];
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component ==0) {
[pickerView reloadComponent:1];
[pickerView selectRow:0inComponent:1animated:YES];
}
// 获得选中的省份名称
int index = [self.picker selectedRowInComponent:0];
SUNCityInfo *cityInfo = self.cities[index];
int cIndex = [self.picker selectedRowInComponent:1];
self.cityLabel.text = [NSString stringWithFormat:@"%@ %@",cityInfo.name,cityInfo.cities[cIndex]] ;
}
城市选择功能实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。