一种带索引城市选择器的设计

版本记录

版本号 时间
V1.0 2017.04.12

前言

很多时候我们都需要城市索引列表。做法有很多,下面我就说一下我做的一种按照第一个汉字字母排序的城市列表,看下面的gif图。

城市选择器

详细设计

先看一下文档组织结构。

Snip20170412_1.png

下面看详细代码

1. AppDelegate.m
#import "AppDelegate.h"
#import "JJCityListDisplayVC.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    JJCityListDisplayVC *cityListVC = [[JJCityListDisplayVC alloc] init];
    self.window.rootViewController = cityListVC;
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {

}

@end

2. JJCityListDisplayVC.m
#import "JJCityListDisplayVC.h"
#import "JJCityListDisplayView.h"

@interface JJCityListDisplayVC ()

@end

@implementation JJCityListDisplayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    JJCityListDisplayView *cityDisplayView = [[JJCityListDisplayView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:cityDisplayView];
}

@end

3. JJCityListDisplayView.m
#import "JJCityListDisplayView.h"

static NSString *cellReuseIdentify = @"cellReuseIdentify";

@interface JJCityListDisplayView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSDictionary *cityListDict;
@property (nonatomic, strong) NSArray *keyArr;
@property (nonatomic, strong) UITableView *cityTableView;

@end

@implementation JJCityListDisplayView

#pragma mark - Override Base Function

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self loadCityData];
        [self setupUI];
    }
    return self;
}

- (void)layoutSubviews
{
    self.cityTableView.frame = self.frame;
}

#pragma mark - Object Private Function

- (void)loadCityData
{
    NSURL *cityPlistURL = [[NSBundle mainBundle] URLForResource:@"cityList.plist" withExtension:nil];
    self.cityListDict = [NSDictionary dictionaryWithContentsOfURL:cityPlistURL];
}

- (void)setupUI
{
    self.cityTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.cityTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellReuseIdentify];
    self.cityTableView.delegate = self;
    self.cityTableView.dataSource = self;
    [self addSubview:self.cityTableView];
    
    self.keyArr = self.cityListDict.allKeys;
    self.keyArr = [self.keyArr sortedArrayUsingSelector:@selector(compare:)];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.keyArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.cityListDict[self.keyArr[section]] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellReuseIdentify forIndexPath:indexPath];
    
    NSString *key = self.keyArr[indexPath.section];
    NSArray *cityArr = self.cityListDict[key];
    cell.textLabel.text = cityArr[indexPath.row];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == self.keyArr.count - 1) {
        return nil;
    }
    else {
        return self.keyArr[section];
    }
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.keyArr];
    [arrM exchangeObjectAtIndex:0 withObjectAtIndex:self.keyArr.count - 1];
    return arrM.copy;
}

@end

结果就不多说了,所有的效果gif图上都已经展现了。

后记

这次写的比较简单,就是一个小玩意,是最近太忙了,写复杂的东西时间有点不够了,最近我也正在翻译别的语言。还是那句话,多谢大家的支持吧。谢谢。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 阿中,青春正当时,但请你足够相信。今天的中国已经不是那个落后就要挨打的国家,他在慢慢的发展,慢慢变强。经过70年的...
    看了你柴知道阅读 296评论 0 0
  • 突然就想到这句话了。 其实也没有多突然,只是下午看了一个电视剧,冰糖炖雪梨,不知道大家看过没有。 里面有一个场景是...
    憋说话呀阅读 204评论 0 1
  • 爱运动的人不断翻新花样,不仅自己锻炼,还要组织团队,大家对赌,模仿007. 还要天天一小时,做不到罚,一年后看看变...
    Jeson杰森阅读 241评论 0 2
  • 第一章:小地狱主 鬼门关是什么样子呢?陈仙儿一直以为,鬼门关应该是那种,一扇古代的那种带凸起的大门,特别特别高的那...
    王猫猫有只狗阅读 737评论 0 6