iOS uses UITableView to select a provincial city from the plist

Source: Internet
Author: User

Recently in a weather application, you need to choose your city. Considering that the city information generally does not change greatly, so from the Internet to find the Chinese city information XML file, the following is the use of a tableview implementation of the region selection code, relatively simple, not explained.
AddressViewController.h file

////AddressViewController.h//Addressdemo////Created by Worthy.zhang on 15/5/29.//Copyright (c) 2015 Worthy.zhang. All rights reserved.//#import <UIKit/UIKit.h> #define Kdisplayprovince 0#define Kdisplaycity 1#define KDISPLAYAREA 2 @interface addressviewcontroller : uiviewcontroller<uitableviewdelegate ,uitableviewdatasource>@property(nonatomic,Strong)UITableView*tableview;@property(nonatomic,Assign)intDisplayType;@property(nonatomic,Strong)Nsarray*provinces;@property(nonatomic,Strong)Nsarray*citys;@property(nonatomic,Strong)Nsarray*areas;@property(nonatomic,Strong)NSString*selectedprovince;//The selected province@property(nonatomic,Strong)NSString*selectedcity;//Selected city@property(nonatomic,Strong)NSString*selectedarea;//Selected area@end

ADDRESSVIEWCONTROLLER.M file

////ADDRESSVIEWCONTROLLER.M//Addressdemo////Created by Worthy.zhang on 15/5/29.//Copyright (c) 2015 Worthy.zhang. All rights reserved.//#import "AddressViewController.h"  @interface addressviewcontroller ()@property(nonatomic,Strong)Nsindexpath*selectedindexpath;//The currently selected Nsindexpath@end @implementation addressviewcontroller -(void) viewdidload{[ SelfConfiguredata]; [ SelfConfigureviews];} -(void) configuredata{if( Self. DisplayType= = Kdisplayprovince) {//Read address dictionary from file        NSString*addresspath = [[NSBundleMainbundle] pathforresource:@"Address"oftype:@"Plist"];nsmutabledictionary*dict = [[nsmutabledictionaryAlloc]initwithcontentsoffile:addresspath]; Self. Provinces= [Dict objectforkey:@"Address"]; }}-(void) configureviews{if( Self. DisplayType= = Kdisplayprovince) {//Show Cancel button only on selected provinces page         Self. Navigationitem. Leftbarbuttonitem= [[Uibarbuttonitemalloc]initwithtitle:@"Cancel"Style:uibarbuttonitemstyleplain Target: SelfAction@selector(cancel)]; }if( Self. DisplayType= = Kdisplayarea) {//Display the OK button only on the Select Area page         Self. Navigationitem. Rightbarbuttonitem= [[Uibarbuttonitemalloc]initwithtitle:@"OK"Style:uibarbuttonitemstyleplain Target: SelfAction@selector(submit)]; }CGRectframe = [ Self. ViewBounds]; Self. TableView= [[UITableViewAlloc]initwithframe:frame]; Self. TableView. Delegate= Self; Self. TableView. DataSource= Self; [ Self. ViewAddsubview: Self. TableView];} -(Nsinteger) TableView: (UITableView*) TableView numberofrowsinsection: (Nsinteger) section{if( Self. DisplayType= = Kdisplayprovince) {return  Self. Provinces. Count; }Else if( Self. DisplayType= = kdisplaycity) {return  Self. Citys. Count; }Else{return  Self. Areas. Count; }}-(UITableViewCell*) TableView: (UITableView*) TableView Cellforrowatindexpath: (Nsindexpath*) indexpath{Static NSString* ID = @"Citycell";UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:id];if(!cell) {cell = [[UITableViewCellAlloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:id];if( Self. DisplayType= = Kdisplayarea) {cell. Accessorytype= Uitableviewcellaccessorynone; }Else{cell. Accessorytype= Uitableviewcellaccessorydisclosureindicator; }    }if( Self. DisplayType= = Kdisplayprovince) {nsdictionary*province = Self. Provinces[Indexpath. Row];NSString*provincename = [Province objectforkey:@"Name"]; Cell. Textlabel. Text= Provincename; }Else if( Self. DisplayType= = kdisplaycity) {nsdictionary*city = Self. Citys[Indexpath. Row];NSString*cityname = [City objectforkey:@"Name"]; Cell. Textlabel. Text= CityName; }Else{cell. Textlabel. Text= Self. Areas[Indexpath. Row]; Cell. ImageView. Image= [UIImageimagenamed:@"Unchecked"]; }returnCell;} -(void) TableView: (UITableView*) TableView Didselectrowatindexpath: (Nsindexpath*) indexpath{if( Self. DisplayType= = Kdisplayprovince) {nsdictionary*province = Self. Provinces[Indexpath. Row];Nsarray*citys = [Province objectforkey:@"Sub"]; Self. Selectedprovince= [Province objectforkey:@"Name"];//Build next-level view ControllerAddressviewcontroller *CITYVC = [[Addressviewcontroller alloc]init]; Cityvc. DisplayType= Kdisplaycity;//display mode for CityCityvc. Citys= Citys; Cityvc. Selectedprovince= Self. Selectedprovince; [ Self. NavigationcontrollerPUSHVIEWCONTROLLER:CITYVC Animated:YES]; }Else if( Self. DisplayType= = kdisplaycity) {nsdictionary*city = Self. Citys[Indexpath. Row]; Self. Selectedcity= [City objectforkey:@"Name"];Nsarray*areas = [City objectforkey:@"Sub"];//Build next-level view ControllerAddressviewcontroller *AREAVC = [[Addressviewcontroller alloc]init]; Areavc. DisplayType= Kdisplayarea;//display mode for areaAreavc. Areas= areas; Areavc. Selectedcity= Self. Selectedcity; Areavc. Selectedprovince= Self. Selectedprovince; [ Self. NavigationcontrollerPUSHVIEWCONTROLLER:AREAVC Animated:YES]; }Else{//Cancel last selected state        UITableViewCell*oldcell = [TableView cellforrowatindexpath: Self. Selectedindexpath]; Oldcell. ImageView. Image= [UIImageimagenamed:@"Unchecked"];//Tick the currently selected state        UITableViewCell*newcell = [TableView Cellforrowatindexpath:indexpath]; Newcell. ImageView. Image= [UIImageimagenamed:@"Checked"];//Save         Self. Selectedarea= Self. Areas[Indexpath. Row]; Self. Selectedindexpath= Indexpath; }}-(void) submit{NSString*msg = [NSStringstringwithformat:@"%@-%@-%@", Self. Selectedprovince, Self. Selectedcity, Self. Selectedarea]; Uialertview *alert = [[Uialertview alloc]initwithtitle:@"Select Address"Message:msg Delegate:Nilcancelbuttontitle:@"Cancel"Otherbuttontitles:Nil,Nil]; [Alert show];} -(void) cancel{[ SelfDismissviewcontrolleranimated:YESCompletion:Nil];}@end

As follows:


Demo Address:
Https://github.com/WorthyZhang/AddressDemo
Provincial Urban plist data:
http://download.csdn.net/detail/huaxm/8468095

iOS uses UITableView to select a provincial city from the plist

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.