首先导入AFNetworking、MJExtension、SDWebimage这三个SDK,在ViewController.m中导入头文件。在infon.plist中开网。创建继承于NSObject的两个model类,在oneModel.h中定义变量
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSArray *imgs;
在twoModel.h中定义变量
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSString *pic;
在ViewController中导入oneModel与twoModel的头文件。
在ViewController.m中写网格
遵守协议
定义全局变量
// 网格
@property(nonatomic,strong)UICollectionView *collV;
@property(nonatomic,strong)NSMutableArray *oneData;
@property(nonatomic,strong)NSMutableArray *twoData;
实现
// 初始化数组
self.oneData = [[NSMutableArray alloc]init];
self.twoData = [[NSMutableArray alloc]init];
// 创建流水布局 固定网格单元格的位置
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
// 设置单元格大小
layout.itemSize = CGSizeMake((self.view.frame.size.width) / 3 - 1, (self.view.frame.size.height) / 3 - 1);
// 设置最小行间距
layout.minimumLineSpacing = 0;
// 设置最小列间距
layout.minimumInteritemSpacing = 0;
// 设置分区间距
layout.sectionInset = UIEdgeInsetsMake(0, 0, 10, 0);
// 创建网格
self.collV = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
self.collV.backgroundColor = [UIColor whiteColor];
// 设置代理
self.collV.delegate = self;
self.collV.dataSource = self;
// 注册cell
[self.collV registerClass:[oneCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
// 添加到视图
[self.view addSubview:self.collV];
实现数据源方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (section == 0) {
return self.oneData.count;
}else{
return self.twoData.count;
}
}
在写cell内容之前,首先创建继承于UICollectionViewCell的oneCollectionViewCell文件,并在ViewCOntroller.m中导入头文件
在oneCollectionViewCell.h中定义
@property(nonatomic,strong)UIImageView *imgV;
@property(nonatomic,strong)UILabel *titleLabel;
在oneCollectionViewCell.m中实现方法,写懒加载
// 初始化方法
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame: frame]) {
[self addSubview:self.imgV];
[self addSubview:self.titleLabel];
}
return self;
}
-(UIImageView *)imgV{
if (!_imgV) {
_imgV = [[UIImageView alloc]init];
}
return _imgV;
}
-(UILabel *)titleLabel{
if (!_titleLabel) {
_titleLabel = [[UILabel alloc]init];
}
return _titleLabel;
}
// 设置位置 布局子控件
-(void)layoutSubviews{
[super layoutSubviews];
self.imgV.frame = CGRectMake(0, 0, 80, 80);
self.titleLabel.frame = CGRectMake(0, 80, 80, 20);
}
// 设置内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
oneCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
if (indexPath.section == 0) {
// 获取数据
oneModel *m1 = self.oneData[indexPath.row];
[cell.imgV sd_setImageWithURL:[NSURL URLWithString:m1.imgs.lastObject]];
cell.titleLabel.text = m1.title;
}else{
// 获取数据
twoModel *m2 = self.twoData[indexPath.row];
[cell.imgV sd_setImageWithURL:[NSURL URLWithString:m2.pic]];
cell.titleLabel.text = m2.title;
}
return cell;
}
在ViewController.m中定义请求网络数据的方法
// 请求网络数据方法
[self sendRequest];
// 请求第二个网络数据
[self sendTwoRequest];
实现网络数据请求方法
-(void)sendRequest{
// 网络请求
// 创建网络请求管理
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://hop.haodou.com/hop/router/rest.json?uuid=e9c30e86e3b5705e6c3e0e7e14ba3ce2&vc=122&_HOP_=%7B%22version%22%3A%221.0%22%2C%22action%22%3A%22front.get_index%22%2C%22secret_id%22%3A%225722f877e4b0d4512e3fd872%22%2C%22current_time%22%3A1510359257%2C%22sign%22%3A%22ed0b6c097b18405bd88e3ad6a9e87128%22%7D&uid=32056801&offset=0&limit=20&appid=4&brand=Apple&channel=appstore&from=iPhone%206&hduid=32056801&idfa=8a40e5398b53b36c415a3ba265f96cf4&isBreak=0&model=iPhone%206&netType=WIFI&osName=iPhone%20OS&osVersion=9.2.1&uid=32056801&uuid=e9c30e86e3b5705e6c3e0e7e14ba3ce2&vc=122&vn=v6.1.35" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSDictionary *dic = responseObject[@"data"];
NSArray *arr = dic[@"list"];
for (NSDictionary *dic in arr) {
// 字典转模型
oneModel *m1 = [oneModel mj_objectWithKeyValues:dic];
// 模型添加到数组
[self.oneData addObject:m1];
}
dispatch_async(dispatch_get_main_queue(), ^{
// 刷新网格
[self.collV reloadData];
});
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error);
}];
}
// 请求第二个网络数据
-(void)sendTwoRequest{
// 网络请求
// 创建网络请求管理
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 包装参数
NSDictionary *dic = @{@"channel":@"头条",@"start":@"0",@"num":@"10",@"appkey":@"de394933e1a3e2db"};
[manager GET:@"http://api.jisuapi.com/news/get" parameters:dic success:^(NSURLSessionDataTask *task, id responseObject) {
NSDictionary *dic = responseObject[@"result"];
NSArray *arr = dic[@"list"];
for (NSDictionary *dic in arr) {
// 字典转模型
twoModel *m2 = [twoModel mj_objectWithKeyValues:dic];
// 模型添加到数组
[self.twoData addObject:m2];
}
dispatch_async(dispatch_get_main_queue(), ^{
// 刷新网格
[self.collV reloadData];
});
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"%@",error);
}];
}