#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSDictionary * provinces;
@property (nonatomic,strong)NSArray * cities;
@property (nonatomic,strong)UITableView * tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取plist文件信息
NSBundle * bundle = [NSBundle mainBundle];
NSURL * url = [bundle URLForResource:@"provinceCities" withExtension:@"plist"];
NSDictionary * dictionary = [NSDictionary dictionaryWithContentsOfURL:url];
_provinces = dictionary;
// 获取字典里的所有key
NSArray * array = [dictionary allKeys];
_cities = array;
// 创建tableView
UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
_tableView = tableView;
[self.view addSubview:tableView];
}
#pragma mark--协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.provinces objectForKey:self.cities[section]] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.provinces.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * Id = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:Id];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Id];
cell.textLabel.text = ([self.provinces objectForKey:self.cities[indexPath.section]])[indexPath.row];
}
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.cities[section];
}
//右侧索引条
-(nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.cities;
}
iOS开发:获取plist文件内容
最新推荐文章于 2019-09-06 11:41:20 发布