IOS学习 UIDatePicker 省市区 三联动

本文详细介绍了如何使用iOS和Swift进行移动应用开发,涵盖了从基础概念到实际项目构建的全过程,包括界面设计、数据管理、网络请求、性能优化等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@interface HomeViewController : UIViewController


@property (copy, nonatomic) NSString *state;

@property (copy, nonatomic) NSString *city;

@property (copy, nonatomic) NSString *district;


@end



@interface HomeViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

{

    NSArray *provinces, *cities, *areas;

}

@end


@implementation HomeViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    //宽和高是固定的,:320,216

    UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-400, 0, 0)];

    pickerView.delegate = self;

    pickerView.dataSource = self;

    pickerView.showsSelectionIndicator = YES;

    [self.view addSubview:pickerView];

    

    //获取plist文件路径,两种方式

    NSBundle *bundle = [NSBundle mainBundle];

//    NSString *path = [[bundle resourcePath] stringByAppendingPathComponent:@"area.plist"]; 

    NSString *path = [bundle pathForResource:@"area" ofType:@"plist"];

    //读取文件内容

    provinces = [[NSArray alloc]initWithContentsOfFile:path];

    cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];

    areas = [[cities objectAtIndex:0] objectForKey:@"areas"];

    

    //设置初始值

    self.state = [[provinces objectAtIndex:0] objectForKey:@"state"];  //

    self.city = [[cities objectAtIndex:0] objectForKey:@"city"];  //

    //地区

    if (areas.count > 0) {

        self.district = [areas objectAtIndex:0];

    } else{

        self.district = @"";

    }

}


//列数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 3;

}


//行数

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    switch (component) {

        case 0:

            return [provinces count];

            break;

        case 1:

            return [cities count];

            break;            

        default:

            return [areas count];

            break;

    }

}


- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    switch (component) {

        case 0:

            return [[provinces objectAtIndex:row] objectForKey:@"state"];

            break;

        case 1:

            return [[cities objectAtIndex:row] objectForKey:@"city"];

            break;

        case 2:

            if ([areas count] > 0) {

                return [areas objectAtIndex:row];

                break;

            }

        default:

            return  @"";

            break;

    }

}


//选择行事件,监听轮子的移动

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    switch (component) {

        case 0:

            cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];

            //将指定的行滚动到中心

            [pickerView selectRow:0 inComponent:1 animated:YES];

            //刷新指定列中的行

            [pickerView reloadComponent:1];

            

            areas = [[cities objectAtIndex:0] objectForKey:@"areas"];

            [pickerView selectRow:0 inComponent:2 animated:YES];

            [pickerView reloadComponent:2];

            

            self.state = [[provinces objectAtIndex:row] objectForKey:@"state"];

            self.city = [[cities objectAtIndex:0] objectForKey:@"city"];

            if ([areas count] > 0) {

                self.district = [areas objectAtIndex:0];

            } else{

                self.district = @"";

            }

            break;

        case 1:

            areas = [[cities objectAtIndex:row] objectForKey:@"areas"];

            [pickerView selectRow:0 inComponent:2 animated:YES];

            [pickerView reloadComponent:2];

            

            self.city = [[cities objectAtIndex:row] objectForKey:@"city"];

            if ([areas count] > 0) {

                self.district = [areas objectAtIndex:0];

            } else{

                self.district = @"";

            }

            break;

        case 2:

            if ([areas count] > 0) {

                self.district = [areas objectAtIndex:row];

            } else{

                self.district = @"";

            }

            break;

        default:

            break;

    }

    

    NSInteger selectedProvinceIndex = [pickerView selectedRowInComponent:0];

    NSString *selectedState = [[provinces objectAtIndex:selectedProvinceIndex]objectForKey:@"state"];

    

    NSInteger selectedCityIndex = [pickerView selectedRowInComponent:1];

    NSString *selectedCity = [[cities objectAtIndex:selectedCityIndex]objectForKey:@"city"];

    

    NSInteger selectedAreaIndex = [pickerView selectedRowInComponent:2];

    NSString *selectedArea = [areas objectAtIndex:selectedAreaIndex];

    

    NSLog(@"State =%@ ,City = %@,Area = %@",selectedState,selectedCity,selectedArea);

}


//设置列宽

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

{

    if (component == 0) {

        return 60;

    }

    return 120;

}


//设置行高

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

    return 60;

}


//自定义行中显示的View

//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{

//    if (component == 0) {

//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];

//        view.backgroundColor = [UIColor cyanColor];

//        return view;

//    }

//    return nil;

//}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值