- 二話不說(shuō)就上圖,大家看看效果先
本篇文章主要涉及講解表視圖和集合視圖和網(wǎng)絡(luò)請(qǐng)求解析數(shù)據(jù)
- 以下的類的創(chuàng)建:
這里我給一個(gè)URL定位符:
http://apis.map.qq.com/ws/district/v1/list?key=K3VBZ-M6WWV-PPSPY-UVGGC-DRM2Z-PGBMV
這里面是全國(guó)34個(gè)省直轄市, 市, 自治區(qū), 市區(qū),街道的詳細(xì)介紹, 今天就講一下網(wǎng)絡(luò)請(qǐng)求的數(shù)據(jù)進(jìn)行數(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
- 咱們先把屬性都準(zhǔn)備好, 再把表視圖和集合視圖的協(xié)議簽好
- (void)getData
{
//請(qǐng)求數(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這個(gè)方法里是網(wǎng)絡(luò)請(qǐ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];
//遍歷當(dāng)前數(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];
}
//請(qǐng)求市后 刷新tableView
[self.cityTableView reloadData];
}
- getcityDataById:這個(gè)方法里是網(wǎng)絡(luò)請(qǐ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];
//遍歷當(dāng)前數(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ǐng)求區(qū)后 刷新tableView
[self.zoneTableView reloadData];
}
- getZoneDatabyId:這個(gè)方法里是網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)的解析區(qū)/街道數(shù)據(jù)信息
- (void)viewDidLoad {
[super viewDidLoad];
//設(shè)置省坐標(biāo)為-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];
//注冊(cè)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];
//注冊(cè)市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];
//注冊(cè)區(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];
//設(shè)置cell顯示內(nèi)容
ProvinceModel *model = [self.dataSourceProvinceArray objectAtIndex:indexPath.row];
cell.proNameLabel.text = model.fullname;
return cell;
}
//item的點(diǎn)擊方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//判斷當(dāng)前省是否正在選中
if (self.currentProvinceIndex != indexPath.row)
{
//獲取當(dāng)前省的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
這里面只有一個(gè)屬性
@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;
}
//設(shè)置空間坐標(biāo)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我們都只寫一個(gè)容錯(cuò)方法就可以
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}
ZoneModel.h
@property(nonatomic, copy)NSString *fullname;
@property(nonatomic, copy)NSString *name;
@property(nonatomic, copy)NSString *id;