美团HD-选择城市
Posted 夜行过客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了美团HD-选择城市相关的知识,希望对你有一定的参考价值。
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-选择城市的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio 仿首页美团切换城市(ListView)+数据库帮助类SQLiteOpenHelper+LetterView(字母排序)