大体实现效果
数据源
·首先判断realm中否有数据,如果没有则从本地json文件中加载数据,并且将数据写至realm数据库中。
RLMResults *result = [CitysRealmModel allObjects];
//从数据库中查询
CitysRealmModel *realmModel = [result firstObject];
if (realmModel) {
self.indexArr = [NSKeyedUnarchiver unarchiveObjectWithData:realmModel.indexData];
self.dataArr = [NSKeyedUnarchiver unarchiveObjectWithData:realmModel.citysData];
[self.tableView reloadData];
}else{
[self loadDataFromFile];
}
//从json文件中取出数据,并且存到realm数据库
-(void)loadDataFromFile{
NSString *file = [[NSBundle mainBundle] pathForResource:@"cityID"ofType:@"json"];
NSData *data=[NSData dataWithContentsOfFile:file];
NSError *errors;
NSDictionary *jsonObject=[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&errors];
NSMutableArray *tmpArr = [NSMutableArray array];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSMutableArray *jsonArr = jsonObject[@"citys"];
for (NSDictionary *dict in jsonArr) {
CitysModel *model = [[CitysModel alloc]init];
[model setValuesForKeysWithDictionary:dict];
[tmpArr addObject:model];
}
for (int i = 0 ; i < tmpArr.count ; i++) {
CitysModel *model = tmpArr[i];
//获取拼音字段首字符,并且转成大写
NSString *firstStr = [[model.pinyin substringToIndex:1]uppercaseString];
//踢出json文件中 全国
if (![model.name isEqualToString:@"全国"]) {
if (self.indexArr.count <=0) {
[self.indexArr addObject:firstStr];
NSMutableArray *arr = [NSMutableArray arrayWithObject:model];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:arr,firstStr, nil];
[self.dataArr addObject:dic];
}else{
if ([self.indexArr containsObject:firstStr]) {
NSMutableArray *array = self.dataArr[0][firstStr];
[array addObject:model];
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:array,firstStr, nil];
[self.dataArr addObject:dic];
}else{
[self.indexArr addObject:firstStr];
NSMutableArray *array = [NSMutableArray arrayWithObject:model];
NSMutableDictionary *dic = self.dataArr[0];
[dic setObject:array forKey:firstStr];
[self.dataArr addObject:dic];
}
}
}
}
NSArray *compareArray = [self.indexArr sortedArrayUsingSelector:@selector(compare:)];
self.indexArr = [NSMutableArray arrayWithArray:compareArray];
NSData *indexData = [NSKeyedArchiver archivedDataWithRootObject:self.indexArr];
/*
//需要在modle文件中实现
-(void)encodeWithCoder:(NSCoder *)aCoder
-(instancetype)initWithCoder:(NSCoder *)aDecoder
*/
NSData *citysData = [NSKeyedArchiver archivedDataWithRootObject:self.dataArr];
//保存数据至数据库
CitysRealmModel *model = [[CitysRealmModel alloc]init];
model.indexData = indexData;
model.citysData = citysData;
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm addObject:model];
[realm commitWriteTransaction];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
});
}
tableview代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.indexArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *listArr = [self.dataArr[0]objectForKey:self.indexArr[section]] ;
return listArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:0 reuseIdentifier:@"cell"];
}
NSArray *listArr = [self.dataArr[0]objectForKey:self.indexArr[indexPath.section]];
CitysModel *model = listArr[indexPath.row];
cell.textLabel.text = model.name;
return cell ;
}
//设置sectionHead标题高度及文字
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.indexArr[section];
}
//设置索引栏文字
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.indexArr;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSArray *listArr = [self.dataArr[0]objectForKey:self.indexArr[indexPath.section]];
CitysModel *model = listArr[indexPath.row];
NSLog(@"%@",model.name);
}