City selector,

Source: Internet
Author: User

City selector,

Effect

 

Source code

Https://github.com/YouXianMing/Animations

/// CustomCityPickerViewController. m // Animations /// Created by YouXianMing on. // Copyright 2017 YouXianMing. all rights reserved. // # import "mcmcitypickerviewcontroller. h "# import" PickerCityDataManager. h "# import" FileManager. h "# import" NSData + JSONData. h "# import" CustomPickerView. h "# import" PickerViewDataAdapter. h "# import" UIView + SetRect. h "# import" PickerProvinceView. h "# import" PickerCityView. h "# import" PickerAreaView. h "@ interface mcmcitypickerviewcontroller () <CustomPickerViewDelegate> @ property (nonatomic, strong) NSArray <PickerProvinceModel *> * provinces; @ property (nonatomic, strong) CustomPickerView * pickerView; @ property (nonatomic, strong) PickerViewDataAdapter * pickerViewDataAdapter; @ end @ implementation CustomCityPickerViewController-(void) viewDidLoad {[super viewDidLoad]; // Get data. NSData * data = [NSData dataWithContentsOfFile: [FileManager bundleFileWithName: @ "city. json "]; NSArray * provinces = [data toListProperty]; self. provinces = [PickerCityDataManager provinceModelsWithArray: provinces]; // Create CustomPickerView's dataAdapter. self. pickerViewDataAdapter = [PickerViewDataAdapter pickerViewDataAdapterWithComponentsBlock: ^ (NSMutableArray <PickerViewComponent *> * components) {// Province component. pickerViewComponent * provinceComponent = [PickerViewComponent pickerViewComponentWithRowsBlock: ^ (NSMutableArray <PickerViewRow *> * rows) {[self. provinces Summary: ^ (PickerProvinceModel * provinceModel, NSUInteger idx, BOOL * _ Nonnull stop) {[rows addObject: [PickerViewRow pickerViewRowWithViewClass: [PickerProvinceView class] data: provinceModel];} componentWidth: Width/3.f-5.f]; // City component. pickerViewComponent * cityComponent = [PickerViewComponent pickerViewComponentWithRowsBlock: ^ (NSMutableArray <PickerViewRow *> * rows) {[self. provinces. firstObject. cities attributes: ^ (PickerCityModel * cityModel, NSUInteger idx, BOOL * _ Nonnull stop) {[rows addObject: [PickerViewRow pickerViewRowWithViewClass: [PickerCityView class] data: cityModel] ;}] ;}componentwidth: Width/3.f-5.f]; // Area component. pickerViewComponent * areaComponent = [PickerViewComponent pickerViewComponentWithRowsBlock: ^ (NSMutableArray <PickerViewRow *> * rows) {[self. provinces. firstObject. cities. firstObject. areas parameters: ^ (PickerAreaModel * areaModel, NSUInteger idx, BOOL * _ Nonnull stop) {[rows addObject: [PickerViewRow pickerViewRowWithViewClass: [PickerAreaView class] data: areaModel] ;};}componentwidth: Width/3.f-5.f]; [components addObject: provinceComponent]; [components addObject: cityComponent]; [components addObject: areaComponent];} rowHeight: 60.f]; // Create CustomPickerView. self. pickerView = [[CustomPickerView alloc] initWithFrame: CGRectMake (0, 0, Width, 0) delegate: self pickerViewHeightType: kCustomPickerViewHeightTypeMax dataAdapter: self. pickerViewDataAdapter]; self. pickerView. center = self. contentView. middlePoint; [self. contentView addSubview: self. pickerView] ;}# pragma mark-CustomPickerViewDelegate-(void) customPickerView :( CustomPickerView *) pickerView didSelectRow :( NSInteger) row inComponent :( NSInteger) component {if (component = 0) {NSMutableArray * citys = [NSMutableArray array]; [self. provinces [row]. cities attributes: ^ (PickerCityModel * cityModel, NSUInteger idx, BOOL * _ Nonnull stop) {[citys addObject: [PickerViewRow attributes: [PickerCityView class] data: cityModel];}]; NSMutableArray * areas = [NSMutableArray array]; [self. provinces [row]. cities. firstObject. areas metadata: ^ (PickerAreaModel * areaModel, NSUInteger idx, BOOL * _ Nonnull stop) {[areas addObject: [PickerViewRow metadata: [PickerAreaView class] data: areaModel];}]; pickerView. pickerViewDataAdapter. components [1]. rows = citys; pickerView. pickerViewDataAdapter. components [2]. rows = areas; [pickerView reloadComponent: 1]; [pickerView reloadComponent: 2]; [pickerView selectRow: 0 inComponent: 1 animated: YES]; [pickerView selectRow: 0 inComponent: 2 animated: YES];} else if (component = 1) {NSInteger provinceIndex = [self. pickerView selectedRowInComponent: 0]; NSMutableArray * areas = [NSMutableArray array]; // Protect crash. if (self. provinces [provinceIndex]. cities. count <= row) {row = self. provinces [provinceIndex]. cities. count-1;} [self. provinces [provinceIndex]. cities [row]. areas metadata: ^ (PickerAreaModel * areaModel, NSUInteger idx, BOOL * _ Nonnull stop) {[areas addObject: [PickerViewRow metadata: [PickerAreaView class] data: areaModel];}]; pickerView. pickerViewDataAdapter. components [2]. rows = areas; [pickerView reloadComponent: 2]; [pickerView selectRow: 0 inComponent: 2 animated: YES]; }}- (void) customPickerView :( CustomPickerView *) pickerView didSelectedRows :( NSArray <NSNumber *> *) rows selectedDatas :( NSArray <id> *) datas {NSLog (@ "% @", datas);} @ end

 

Features

1. Each row can be customized like a cell using UITableView. inherit CustomPickerView.

2. The data source is specified through the PickerViewDataAdapter. Each PickerViewComponent has an array of PickViewRow.Each PickViewRow element corresponds to a customCustomPickerView.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.