效果图:
接口:左边接口、section0接口、section1接口 。
需求:左边的数据需要对应右边的数据
控制器层:
1. 网络请求
- 1.如果是刷新数据,就将原来的数据删除
- 2.封装数据模型
- 3.刷新界面
2. 处理数据:
- 1.定义一个不可变数组用来接收数据
- 2.先清空之前数据<如果是第一次请求就不需要>
- 3.addOjbect<如字典中有需要判断的就先add一次>
- 4. 遍历数据
- 5.addobject<判断后再次存储>
3. 懒加载:
- 懒加载数据源
- 懒加载视图
- 使用block处理回调
具体细节:
- 请求网络[requeatDataFromNetWork]//左边接口
- 1.1如果是刷新数据,就将原来的数据删除
- 1.2封装左边TableView数据模型
- 1.3刷新界面
- demo
[HTTPRequest GET:kClassDetailURL parameter:nil success:^(id resposeObject) {
[self handleLeftResponseObjectData:resposeObject];
self.leftView.leftDataArr = self.leftDataArr;
} failure:^(NSError *error) {
NSLog(@"%@",error);
}];
- 处理左边tableView的数据源
- 1.定义一个不可变数组用来接收数据
- 2.遍历数据
- 3.addObject
- demo
//处理左边tableView的数据源
- (void)handleLeftResponseObjectData:(id)responseobject{
NSArray *sourceArr = responseobject;
for (NSDictionary *dic in sourceArr) {
//获取分类的数组 (level 有: 1, 2, 3) level : 1 共14个
if ([dic[@"level"]integerValue ] == 1) {
ZBClassLeftModel *model = [[ZBClassLeftModel alloc]initWithDic:dic];
[self.leftDataArr addObject:model];
}
}
//默认选中第一个分类
[self.leftDataArr[0] setDidSelected:YES];
//默认右边collcetion加载第一个分类的数据
[self requectRightSectionUpDataFromNetWork:[self.leftDataArr firstObject]];
//默认右边collection的第二组加载第一个分组的数据
[self requestRightSectionDownDataFromNetWork:[self.leftDataArr firstObject]];
// HHLog(@"tableView的种类有%ld", self.leftDataArr.count);
}
- 网络请求右边section0接口
[HTTPRequest GET:kClassDetailURL parameter:nil success:^(id resposeObject) {
HHLog(@"rightSectionUpData%@",resposeObject);
//1.封装右边collectionView的section 0 数据模型
[self handleRightUpSectionObjectData:resposeObject LeftModel:leftModel];
//2.刷新collectionView界面
self.rightView.rightSectionUpaDataArr = self.rightSectionUpDataArr;
} failure:^(NSError *error) {
NSLog(@"%@",error);
}];
- 处理右边collectionView secetion0的数据模型
- 1.定义一个不可变数组用来接收数据
- 2.先清空之前数据
- 3.addOjbect
- 4.遍历数据
- 5.addOjbect
NSArray *soucreArr = responseobject;
//1.清除之前的数据
[self.rightSectionUpDataArr removeAllObjects];
//2.先把总分类加上
[self.rightSectionUpDataArr addObject:leftmodel];
for (NSDictionary *dic in soucreArr) {
if ([dic[@"parent_url_name"]isEqualToString:leftmodel.url_name]) {
ZBClassLeftModel *model = [[ZBClassLeftModel alloc]initWithDic:dic];
[self.rightSectionUpDataArr addObject:model];
}
}
}
- 网络请求secetion1接口
- (void)requestRightSectionDownDataFromNetWork:(ZBClassLeftModel*)leftModel{
[HTTPRequest GET:kClassBrandURL parameter:nil success:^(id resposeObject) {
//1.封装右边collcetionView的section1数据模型
[self handleRightDownSectionObjectionData:resposeObject leftModel:leftModel];
self.rightView.rightSectionDownaDataArr = self.rightSectionDownDataArr;
} failure:^(NSError *error) {
}];
}
- 处理右边collectionView secetion1的数据模型
- (void)handleRightDownSectionObjectionData:(id)responseObject leftModel:(ZBClassLeftModel*)leftModel{
NSArray *objArrs = responseObject[@"objects"];
// 清空原来的数据
[self.rightSectionDownDataArr removeAllObjects];
for (NSDictionary *dic in objArrs) {
if ([dic[@"brand_url_name"] isEqualToString:leftModel.url_name]) {
ZBClassRightSectionDownModel *model = [[ZBClassRightSectionDownModel alloc]initWithDic:dic];
[self.rightSectionDownDataArr addObject:model];
}
}
}
- 懒加载
//左边的TableView
-(ZBClassLeftView *)leftView{
if (!_leftView) {
ZBClassLeftView *leftView = [[ZBClassLeftView alloc]initWithFrame:CGRectMake(0, 0, LeftViewWidth, self.view.frame.size.height -49)];
//回调用于获取collectionViewSection0的数据
[leftView setClassLeftSelectedCallBack:^(ZBClassLeftModel *leftModel) {
//点击左边的tableView的cell后请求后边的collection的数据
[self requectRightSectionUpDataFromNetWork:leftModel];
//同时请求右边collection的section1的数据
[self requestRightSectionDownDataFromNetWork:leftModel];
}];
[self.view addSubview:leftView];
_leftView = leftView;
}
return _leftView;
}
//右边的TableView
-(ZBClassRightView *)rightView{
if (!_rightView) {
ZBClassRightView *rightView = [[ZBClassRightView alloc]initWithFrame:CGRectMake(LeftViewWidth, 0, RightViewWidth, self.view.frame.size.height -49)];
//点击了section0的cell回调
[rightView setClassRightSectionUpCellCallBack:^(NSString *query, NSString *category_name) {
if (![query isEqualToString:@""]) {
ZBClassLeftUpViewController *leftUpVC = [[ZBClassLeftUpViewController alloc]initWithQueryString:query category_name:category_name];
leftUpVC.view.frame = self.view.frame;
[self.navigationController pushViewController:leftUpVC animated:YES];
}
}];
//点击了section1的cell回调
[rightView setClassRightSectionDownCellCallBack:^(NSString *begin_time, NSString *end_time, NSString *idString, NSString *special_name) {
if (![idString isEqualToString:@""]) {
ZBClassRightDownDetaViewController *RightDownDataVC = [[ZBClassRightDownDetaViewController alloc]initWithID:idString special_name:special_name begin_time:begin_time end_time:end_time];
RightDownDataVC.view.frame = self.view.frame;
[self.navigationController pushViewController:RightDownDataVC animated:YES];
}
}];
[self.view addSubview:rightView];
_rightView = rightView;
}
return _rightView;
}
-(NSMutableArray *)leftDataArr{
if (!_leftDataArr) {
_leftDataArr = [NSMutableArray array];
}
return _leftDataArr;
}
//右边collectionView的section 0 数据源
-(NSMutableArray *)rightSectionUpDataArr{
if (!_rightSectionUpDataArr) {
_rightSectionUpDataArr = [NSMutableArray array];
}
return _rightSectionUpDataArr;
}
//右边collectionView的section 1 数据源
-(NSMutableArray *)rightSectionDownDataArr{
if (!_rightSectionDownDataArr) {
_rightSectionDownDataArr = [NSMutableArray array];
}
return _rightSectionDownDataArr;
}