首页 > 代码库 > 美团HD(5)-选择城市
美团HD(5)-选择城市
DJSelectCityViewController.m
#import "DJSelectCityViewController.h"#import "DJConstantValue.h"#import "DJCityGroup.h"#import "MJExtension.h"@interface DJSelectCityViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>/** 城市组列表 */@property (nonatomic,strong) NSMutableArray *cityGroups;@property (weak, nonatomic) IBOutlet UITableView *cityTableView;@end@implementation DJSelectCityViewController- (void)viewDidLoad { [super viewDidLoad]; self.title = @"选择城市"; // 设置右侧索引栏字体颜色 self.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;}/** 加载城市数据 */- (void)loadCityData { self.cityGroups = [DJCityGroup mj_objectArrayWithFilename:@"cityGroups.plist"]; }/** 关闭当前界面 */- (void)close { [self dismissViewControllerAnimated:YES completion:nil];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - UITableView 数据源方法- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.cityGroups.count;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { DJCityGroup *cityGroup = self.cityGroups[section]; return cityGroup.cities.count; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *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; return cell;}#pragma mark - tableView 代理方法- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { DJCityGroup *cityGroup = self.cityGroups[section]; return cityGroup.title; }- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [self.cityGroups valueForKeyPath:@"title"];}#pragma mark - UISearchBar 代理方法/** SearchBar开始编辑 */- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { // 隐藏导航栏 [self.navigationController setNavigationBarHidden:YES animated:YES]; }/** SearchBar结束编辑 */- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { // 显示导航栏 [self.navigationController setNavigationBarHidden:NO animated:YES];}@end
最终效果:
美团HD(5)-选择城市
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。