iOS 地区索引列表

大体实现效果

EDE90487-EA8E-49EE-9680-A7FE844B3B04.png

数据源

·首先判断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);
    
    
}

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

推荐阅读更多精彩内容