//
// ViewController.m
// city
//
// Created by imac on 15/8/20.
// Copyright (c) 2015年 刘
. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource>
@property(nonatomic,strong)NSDictionary *dataDic;
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
// _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
[self.view addSubview:_tableView];
_tableView.dataSource = self;
[self dataArray];
[self dataDic];
}
#pragma mark 城市数据
-(NSDictionary *)dataDic{
NSString *path = [[NSBundle mainBundle] pathForResource:@"cities.plist" ofType:nil];
_dataDic = [NSDictionary dictionaryWithContentsOfFile:path];
return _dataDic;
}
#pragma mark 省级数据
-(NSArray *)dataArray{
NSString *path = [[NSBundle mainBundle]pathForResource:@"provinces.plist" ofType:nil];
_dataArray = [NSArray arrayWithContentsOfFile:path];
return _dataArray;
}
#pragma mark 分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dataArray.count;
}
#pragma mark 每组个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = _dataArray[section];
NSArray *arrTemp = [NSArray array];
arrTemp = [_dataDic valueForKey:key];
return arrTemp.count;
}
#pragma mark 每行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
NSString *key = _dataArray[indexPath.section];
NSArray *arrTemp = [NSArray array];
arrTemp = [_dataDic valueForKey:key];
cell.textLabel.text = arrTemp[indexPath.row];
return cell;
}
#pragma mark 每组的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return _dataArray[section];
}
@end
需要使用到两个plist的文件,进行本地数据加载