网格解析

1.导入AFNetworking第三方
2.在Info.plist中添加:
在这里插入图片描述
3.创建NewViewController继承于UIViewController
在NewViewController.h中添加:
@property(nonatomic,strong)UIImage *img;
@property(nonatomic,strong)NSString *name;

在NewViewController.m中:
#import “NewViewController.h”
@interface NewViewController ()
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;
@end
@implementation NewViewController
-(UIImageView *)imageView{

if (!_imageView) {
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50)];
}
return _imageView;

}
-(UILabel *)label{

if (!_label) {
    _label = [[UILabel alloc]initWithFrame:CGRectMake(200, self.view.frame.size.height-40, 200, 30)];
}
return _label;

}
-(void)viewDidLoad {

[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.imageView];
[self.view addSubview:self.label];
self.imageView.image = self.img;
self.label.text = self.name;

}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[self dismissViewControllerAnimated:YES completion:nil];

}
-(void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

}
@end

4.创建MyCollectionViewCell继承于UICollectionViewCell
在MyCollectionViewCell.h中添加:
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;

在MyCollectionViewCell.m中:
#import “MyCollectionViewCell.h”
@implementation MyCollectionViewCell
-(UIImageView *)imageView{

if (!_imageView) {
    _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 10, 60, 50)];
    
}
return _imageView;

}
-(UILabel *)label{

if (!_label) {
    _label = [[UILabel alloc]initWithFrame:CGRectMake(30, 70, 80, 20)];
}
return _label;

}
-(instancetype)initWithFrame:(CGRect)frame{

if (self = [super initWithFrame:frame]) {
    [self addSubview:self.imageView];
    [self addSubview:self.label];
}
return self;

}
@end

5.在ViewController.m中:
#import “ViewController.h”
#import “MyCollectionViewCell.h”
#import “NewViewController.h”
#import “AFNetworking/AFNetworking.h”
#define TEST_URL @“http://192.168.100.7/1608E1.json
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>{

NSDictionary *_dic;

}
@property(nonatomic,strong)UICollectionView *myCollection;
@end
@implementation ViewController
//重写get方法
-(UICollectionView *)myCollection{

if (!_myCollection) {
    //初始化布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置每个模块的大小
    layout.itemSize = CGSizeMake(100, 100);
    //设置滚动方向
    //        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //设置最小行间距
    layout.minimumLineSpacing = 10;
    //设置最小列间距
    layout.minimumInteritemSpacing = 5;
    //设置头视图的大小 自动适配  (如果是垂直滚动的话 宽自动是屏幕的宽,可以设置高度。反之如果是水平滚动的话 高自动是屏幕的高,可以设置宽度。)
    layout.headerReferenceSize = CGSizeMake(100, 30);
    //设置尾视图的大小
    layout.footerReferenceSize = CGSizeMake(100, 30);
    //设置每个模块中 上 左 下 右的间距(模块的边界和模块内容的距离)
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //        _myCollection  = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    _myCollection  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    
    //设置代理
    _myCollection.delegate = self;
    _myCollection.dataSource = self;
    //注册单元格
    [_myCollection registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCellId"];
}
return _myCollection;

}
-(void)viewDidLoad {

[super viewDidLoad];
[self.view addSubview:self.myCollection];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [[AFJSONResponseSerializer alloc]init];
NSURL *url = [NSURL URLWithString:TEST_URL];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    
    self->_dic = responseObject;
    [self->_myCollection reloadData];
}];
[task resume];

}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

return _dic.count;

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

NSString *str = _dic.allKeys[section];
return [[_dic objectForKey:str] count];

}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellId =@"collectionCellId";
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId  forIndexPath:indexPath];


NSString *str = _dic.allKeys[indexPath.section];

NSString *imgstr = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"img"];
NSURL *urlimg = [NSURL URLWithString:imgstr];
NSData *data = [NSData dataWithContentsOfURL:urlimg];
cell.imageView.image = [[UIImage alloc]initWithData:data];

cell.label.text =[[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"name"];
cell.backgroundColor = [UIColor redColor];
return cell;

}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"第%ld分区---第%ld行",indexPath.section,indexPath.row);
NewViewController  *new = [[NewViewController alloc]init];

NSString *str = _dic.allKeys[indexPath.section];

NSString *imgstr = [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"img"];
NSURL *urlimg = [NSURL URLWithString:imgstr];
NSData *data = [NSData dataWithContentsOfURL:urlimg];
new.img = [[UIImage alloc]initWithData:data];
new.name= [[[_dic objectForKey:str]objectAtIndex:indexPath.row]objectForKey:@"name"];

[self presentViewController:new animated:YES completion:nil];

}
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值