iOS通過網(wǎng)絡請求解析數(shù)據(jù)_中國省市區(qū)街道

  • 二話不說就上圖,大家看看效果先
上海市
云南省
本篇文章主要涉及講解表視圖和集合視圖和網(wǎng)絡請求解析數(shù)據(jù)
  • 以下的類的創(chuàng)建:

這里我給一個URL定位符:

http://apis.map.qq.com/ws/district/v1/list?key=K3VBZ-M6WWV-PPSPY-UVGGC-DRM2Z-PGBMV

這里面是全國34個省直轄市, 市, 自治區(qū), 市區(qū),街道的詳細介紹, 今天就講一下網(wǎng)絡請求的數(shù)據(jù)進行數(shù)據(jù)解析


ViewController.m

@interface ViewController ()<UICollectionViewDelegate,       UICollectionViewDataSource, UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain)NSMutableArray *dataSourceProvinceArray;
/**省的集合數(shù)組*/
@property(nonatomic, retain)UICollectionView *collectionView;
/**市的表視圖*/
@property(nonatomic, retain)UITableView *cityTableView;
/**區(qū)的表視圖*/
@property(nonatomic, retain)UITableView *zoneTableView;
/**市的集合數(shù)組*/
@property(nonatomic, retain)NSMutableArray *dataSourceCityArray;
/**區(qū)的集合數(shù)組*/
@property(nonatomic, retain)NSMutableArray *dataSourceZoneArray;
/***/
@property(nonatomic, assign)NSInteger currentProvinceIndex;
@end

@implementation ViewController
  • 咱們先把屬性都準備好, 再把表視圖和集合視圖的協(xié)議簽好
- (void)getData
{
    //請求數(shù)據(jù)并解析
    NSString *strURL = @"http://apis.map.qq.com/ws/district/v1/list?key=K3VBZ-M6WWV-PPSPY-UVGGC-DRM2Z-PGBMV";
    NSURL *URL = [NSURL URLWithString:strURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    //數(shù)據(jù)源數(shù)組:
    self.dataSourceProvinceArray = [NSMutableArray array];
    NSArray *arr = [dic valueForKey:@"result"];
    for (NSDictionary *dic in arr[0])
    {
        ProvinceModel *model = [[ProvinceModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        NSLog(@"%@ * %@", model.fullname, model.id);
        [self.dataSourceProvinceArray addObject:model];
        [model release];
    }
    //collectionView的數(shù)據(jù)刷新
}
  • getData這個方法里是網(wǎng)絡請求數(shù)據(jù)的解析省份數(shù)據(jù)信息
- (void)getcityDataById:(NSString *)proID
{
    NSString *urlString = [NSString stringWithFormat:@"http://apis.map.qq.com/ws/district/v1/getchildren?&id=%@&key=K3VBZ-M6WWV-PPSPY-UVGGC-DRM2Z-PGBMV", proID];
    NSURL *URL = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *allArray = [dic objectForKey:@"result"];
    NSArray *array = [allArray objectAtIndex:0];
    //遍歷當前數(shù)組給madel賦值
    self.dataSourceCityArray = [NSMutableArray array];
    for (NSDictionary *diction in array)
    {
        CityModel *model = [[CityModel alloc] init];
        [model setValuesForKeysWithDictionary:diction];
        [self.dataSourceCityArray addObject:model];
        [model release];
    }
    //請求市后 刷新tableView
    [self.cityTableView reloadData];
}
  • getcityDataById:這個方法里是網(wǎng)絡請求數(shù)據(jù)的解析市數(shù)據(jù)信息
- (void)getZoneDatabyId:(NSString *)cityID
{
    NSString *urlString = [NSString stringWithFormat:@"http://apis.map.qq.com/ws/district/v1/getchildren?&id=%@&key=K3VBZ-M6WWV-PPSPY-UVGGC-DRM2Z-PGBMV", cityID];
    NSURL *URL = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *allArray = [dic objectForKey:@"result"];
    NSArray *array = [allArray objectAtIndex:0];
    //遍歷當前數(shù)組給madel賦值
    self.dataSourceZoneArray = [NSMutableArray array];
    for (NSDictionary *diction in array)
    {
        ZoneModel *model = [[ZoneModel alloc] init];
        [model setValuesForKeysWithDictionary:diction];
        [self.dataSourceZoneArray addObject:model];
        [model release];
    }
    //請求區(qū)后 刷新tableView
    [self.zoneTableView reloadData];
}
  • getZoneDatabyId:這個方法里是網(wǎng)絡請求數(shù)據(jù)的解析區(qū)/街道數(shù)據(jù)信息
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //設置省坐標為-1
    self.currentProvinceIndex = -1;
    
    //解析數(shù)據(jù):
    [self getData];
    
    //初始化瀑布流flowLayout:
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 50);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //初始化collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 300) collectionViewLayout:flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    
    //注冊collectionView的cell
    [self.collectionView registerClass:[ProvinceCell class] forCellWithReuseIdentifier:@"proCell"];
    
    //初始化市視圖
    self.cityTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _collectionView.frame.origin.y + _collectionView.frame.size.height, self.view.frame.size.width / 2.0, self.view.frame.size.height - 20 - _collectionView.frame.size.height) style:UITableViewStylePlain];
    self.cityTableView.delegate = self;
    self.cityTableView.dataSource = self;
    [self.view addSubview:_cityTableView];
    [_cityTableView release];
    //注冊市tableView的cell
    [self.cityTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cityCell"];
    
    
    //初始化區(qū)視圖
    self.zoneTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2.0,  _collectionView.frame.origin.y + _collectionView.frame.size.height,  self.view.frame.size.width / 2.0, self.view.frame.size.height - 20 - _collectionView.frame.size.height) style:UITableViewStylePlain];
    self.zoneTableView.delegate = self;
    self.zoneTableView.dataSource = self;
    [self.view addSubview:_zoneTableView];
    [_zoneTableView release];
    //注冊區(qū)tableView的cell
    [self.zoneTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"zoneCell"];

}
  • 下面是表視圖和集合視圖的一些代理方法:
//--------------------------省collecctionView的代理方法--------------------------//
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceProvinceArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ProvinceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"proCell" forIndexPath:indexPath];
    //設置cell顯示內(nèi)容
    ProvinceModel *model = [self.dataSourceProvinceArray objectAtIndex:indexPath.row];
    cell.proNameLabel.text = model.fullname;
    return cell;
}

//item的點擊方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //判斷當前省是否正在選中
    if (self.currentProvinceIndex != indexPath.row)
    {
        //獲取當前省的model:
        ProvinceModel *model = [self.dataSourceProvinceArray objectAtIndex:indexPath.item];
        [self getcityDataById:model.id];
        //清空區(qū)的數(shù)組并更新
        [self.dataSourceZoneArray removeAllObjects];
        [self.zoneTableView reloadData];
        
        self.currentProvinceIndex = indexPath.row;
    }
}

//--------------------------城市tableView的代理方法--------------------------//
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _cityTableView)
    {
        return self.dataSourceCityArray.count;
    }
    else
    {
        return self.dataSourceZoneArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _cityTableView)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cityCell"];
        CityModel *model = [self.dataSourceCityArray objectAtIndex:indexPath.row];
        cell.textLabel.text = model.fullname;
        return cell;
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zoneCell"];
        ZoneModel *model = [self.dataSourceZoneArray objectAtIndex:indexPath.row];
        cell.textLabel.text = model.fullname;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _cityTableView)
    {
        CityModel *model = [self.dataSourceCityArray objectAtIndex:indexPath.row];
        [self getZoneDatabyId:model.id];
    }
}

ProvinceCell.h

這里面只有一個屬性

@property(nonatomic, retain)UILabel *proNameLabel;

ProvinceCell.m

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.proNameLabel = [[UILabel alloc] init];
        self.proNameLabel.textAlignment = NSTextAlignmentCenter;
        self.proNameLabel.layer.borderWidth = 0.5;
        self.proNameLabel.layer.cornerRadius = 5;
        self.proNameLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_proNameLabel];
        [_proNameLabel release];
    }
    return self;
}

//設置空間坐標frame
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.proNameLabel.frame = self.bounds;
}

CityModel.h

@property(nonatomic, copy)NSString *fullname;
@property(nonatomic, copy)NSString *name;
@property(nonatomic, copy)NSString *id;

CityModel.m 和 ZoneModel.m

Model我們都只寫一個容錯方法就可以

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}

ZoneModel.h

@property(nonatomic, copy)NSString *fullname;
@property(nonatomic, copy)NSString *name;
@property(nonatomic, copy)NSString *id;

大功告成了, 小伙伴們是不是感覺真神奇! 中國那么大, 我想去看看! 要想去看看, 給個好評先!
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,836評論 6 540
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,275評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,904評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,633評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,368評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,736評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,740評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,919評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,481評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,235評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,427評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,968評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,656評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,055評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,348評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,160評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,380評論 2 379

推薦閱讀更多精彩內(nèi)容

  • 國家電網(wǎng)公司企業(yè)標準(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報批稿:20170802 前言: 排版 ...
    庭說閱讀 11,067評論 6 13
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,714評論 25 708
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結起來就是把...
    Dove_iOS閱讀 27,197評論 30 471
  • 昨晚輾轉反側,終于決定出來走一走。 早上起床,我看著窗外下起了漂泊大雨。一個月都沒有下雨了,沒有想到的是此時此刻竟...
    陪月亮摘星星閱讀 352評論 12 19
  • 作者:張靜如 1、真誠待人,經(jīng)常使用美的措辭。 贊美或者鼓勵別人,能讓對方保持美好的心情,同時傳達美麗的心情。 夸...
    佩盈閱讀 568評論 0 8