码迷,mamicode.com
首页 > 其他好文 > 详细

美团HD(5)-选择城市

时间:2017-01-06 01:11:47      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:美团   sel   code   adc   load   string   分享   ret   hba   

 

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)-选择城市

标签:美团   sel   code   adc   load   string   分享   ret   hba   

原文地址:http://www.cnblogs.com/yongdaimi/p/6254496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!