美团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

最终效果:

时间: 2024-08-05 07:29:27

美团HD(5)-选择城市的相关文章

仿美团实现地域选择(一)

介绍 在开发O2O相关应用的时候,肯定会有定位,选择所在城市,选择地域,然后再向服务器请求该地区的相关数据,这时就需要我们提供一个导向让用户选择所在区域. 看来看去,最终还是选择模仿美团,感觉用户体验最好. <-美团的地域选择看起来是这样的 原理 1.定位我们可以使用第三方API,例如百度地图,腾讯地图等,官方文档写的非常清楚了. 百度地图地址:http://developer.baidu.com/map/index.php?title=androidsdk,这里不再多述,demo也不涉及定位相

1.选择城市

项目介绍 应用截图 主要功能 写的一个APP总结一下 使用车联网api解析天气数据并展示在界面 定位 通知栏 桌面小部件 动态添加删除城市并显示在界面上方便查看 选择城市界面 首先我网上找了个城市json数据信息,然后自己做了个json数据存放在res的raw目录下 然后需要解析出来存放在数据库中,不用每次都去解析json CoolWeatherOpenHelper public class CoolWeatherOpenHelper extends SQLiteOpenHelper { /**

选择城市列表的小Demo

先上源码: 选择城市列表的小Demo 选择城市列表的小Demo,布布扣,bubuko.com

winform 实现选择城市列表

先上图 #region 选择城市 /// <summary> /// 点击字母事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void item_Click(object sender, EventArgs e) { LinkLabel lbl = sender as LinkLab

每天一个JavaScript实例-动态省份选择城市

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-动态省份选择城市</title> <script> var citystore = new Array(); citystore = [[

select省市联动选择城市 asp.net mvc4

本文在 http://www.cnblogs.com/darrenji/p/3606703.html(感谢博主的分享)基础上加入全国各省市,从文件中读取全国省市县,组成省市联动的选择标签 在Model里定义Province 和 City ? 1 2 3 4 5 6 7 8 9 10 11 12 13 public class Province {     public int ID { get; set; }     public string Name { get; set; } } publ

美团HD(9)-监听点击城市

DJSelectCityViewController.h // 点击城市发出通知 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 1. 发送点击的城市名 DJCityGroup *cityGroup = self.cityGroups[indexPath.section]; [[NSNotificationCenter defaultCenter] p

仿美团实现地域选择(二)

介绍 上篇实现了PopupWindow选择地域,这篇介绍如何实现带有首字母的快速索引list,进行城市选择,我也是参考了相关博文才弄出来的,知道了原理,才发现如此简单. 其中有个开源项目可以参考,但与本文实现的方式略有不同. 地址:https://github.com/woozzu/IndexableListView 美团的城市选择看起来是这样的.本例中不包含搜索,有空再模仿研究下. 原理 1.侧边快速索引和首字母直接在framelayout中布局的,也可以用代码动态生成. 2.获取拼音首字写用

服务端如何做国内外选择城市功能

选择只是用户的行为,而服务端需要做好城市的储存,搜索. 国内城市选择效果: 首先只考虑字段之间的关系. 范围是从大到小的:省份--城市--地区 外键关系:一个省份 -- 多个城市:一个城市--多个地区 如何储存: 1. 省份,城市,地区各为一个字段,放在用户的 model 中 2. 只储存最小的.最具体的地点.比如地区.通过地区与其他字段的关系,获取其他字段的值. 国外城市的选择 各个国家的行政地区会存在差异.所以如果要兼容国内国外,就不能使用上面的行政关系. 那么,可以怎么做? 我想在想到的是