Djselectcityviewcontroller.m
#import "DJSelectCityViewController.h"#import "DJConstantValue.h"#import "DJCityGroup.h"#import "MJExtension.h"@interfaceDjselectcityviewcontroller () <UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>/** City Group List*/@property (nonatomic,strong) Nsmutablearray*citygroups, @property (weak, nonatomic) Iboutlet UITableView*Citytableview;@end@implementationDjselectcityviewcontroller- (void) viewdidload {[Super viewdidload]; Self.title=@"Select City"; //set the right index bar font colorSelf.cityTableView.sectionIndexColor =[Uicolor Blackcolor]; [Self setupnavleftitem]; [Self loadcitydata]; }- (void) Setupnavleftitem {Uibarbuttonitem*closeitem = [Uibarbuttonitem itemwithtarget:self action: @selector (Close) Image:@"Btn_navigation_close"Highlighimage:@"Btn_navigation_close_hl"]; Self.navigationItem.leftBarButtonItem=Closeitem;}/** Load City Data*/- (void) Loadcitydata {self.citygroups= [Djcitygroup mj_objectarraywithfilename:@"citygroups.plist"]; }/** Close the current interface*/- (void) Close {[Self dismissviewcontrolleranimated:yes completion:nil];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmaMark-uitableview Data Source Method-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {returnSelf.cityGroups.count;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {Djcitygroup*citygroup =Self.citygroups[section]; returnCityGroup.cities.count; }-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {StaticNSString *id =@"Citygroup"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:id]; if(!cell) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:id]; } Djcitygroup*citygroup =Self.citygroups[indexpath.section]; NSString*cityname =Citygroup.cities[indexpath.row]; Cell.textLabel.text=CityName; returncell;}#pragmaMark-tableview Proxy Method-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section {Djcitygroup*citygroup =Self.citygroups[section]; returnCitygroup.title; }-(nsarray<nsstring *> *) Sectionindextitlesfortableview: (UITableView *) TableView {return[Self.citygroups Valueforkeypath:@"title"];}#pragmaMark-uisearchbar Proxy method/** Searchbar start editing*/- (void) Searchbartextdidbeginediting: (Uisearchbar *) Searchbar {//Hide Navigation bar[Self.navigationcontroller Setnavigationbarhidden:yes Animated:yes]; }/** Searchbar End Edit*/- (void) Searchbartextdidendediting: (Uisearchbar *) Searchbar {//Show navigation bar[Self.navigationcontroller setnavigationbarhidden:no animated:yes];}@end
Final effect:
American Mission HD (5)-Select city