解析自定义网格CollectionView

在viewcontroller.m

#import “MyCollectionViewCell.h”
#import “NewViewController.h”
#import <AFNetworking.h>
#define test @""
@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(200, 200);
//设置滚动方向
// layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    //设置最小行间距
    layout.minimumLineSpacing = 5;
    //设置最小列间距
    layout.minimumInteritemSpacing = 5;
    //设置头视图的大小 自动适配  (如果是垂直滚动的话 宽自动是屏幕的宽,可以设置高度。反之如果是水平滚动的话 高自动是屏幕的高,可以设置宽度。)
    layout.headerReferenceSize = CGSizeMake(10, 30);
    //设置尾视图的大小
    layout.footerReferenceSize = CGSizeMake(10, 30);
    //设置每个模块中 上 左 下 右的间距(模块的边界和模块内容的距离)
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

// _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"];
    //注册头视图单元格

// [_myCollection registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@“header”];
// //注册尾视图单元格
// [_myCollection registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@“footer”];
//设置背景颜色
_myCollection.backgroundColor = [UIColor lightGrayColor];

}
return _myCollection;

}
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.myCollection];

//模拟数据

// _dic =@{@“野兽”?[@“小滨”,@“小李”,@“小蒋”,@“小泽”],
// @“帅哥”?[@“小雨”,@“小惠”,@“小建”,@“小金”,@“小尹”,@“小石头”],
// @“美女”?[@“小白”,@“小玉”,@“小霜”,@“小小白”],
// @“异类”?[@“小黑”,@“陈硕”,@“光头”]
// };
//_dic = @{@“野兽”?[@{@“小滨”?“1.jpg”},@{@“小李”?“2.jpg”},@{@“小蒋”?“5.jpg”},@{@“小泽”?“3.jpg”}],
// @“帅哥”?[@{@“小雨”?“1.jpg”},@{@“小惠”?“3.jpg”},@{@“小建”?“1.jpg”},@{@“小金”?“4.jpg”},@{@“小尹”?“6.jpg”},@{@“小石头”?“1.jpg”}],
// @“美女”?[@{@“小白”?“3.jpg”},@{@“小玉”?“7.jpg”},@{@“小霜”?“4.jpg”},@{@“小小白”?“2.jpg”}],
// @“异类”?[@{@“小黑”?“5.jpg”},@{@“陈硕”?“7.jpg”},@{@“光头”?“6.jpg”}]
// };

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [[AFJSONResponseSerializer alloc] init];

NSURL *url = [NSURL URLWithString:test];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    _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;

}
//设置头视图 和尾视图
//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
// if([kind isEqualToString:UICollectionElementKindSectionHeader]){
// static NSString *cellId = @“header”;
// HeaderCollectionReusableView *headerCell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:cellId forIndexPath:indexPath];
// headerCell.label.text = [NSString stringWithFormat:@“第%ld组”,indexPath.section+1];
// headerCell.backgroundColor = [UIColor yellowColor];
// return headerCell;
// }else{
// static NSString *cellId =@“footer”;
// FooterCollectionReusableView *footerCell = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:cellId forIndexPath:indexPath];
// footerCell.label.text = @“结尾”;
// footerCell.backgroundColor = [UIColor greenColor];
// return footerCell;
// }
//
//}
-(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];

}

-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

再创建一个UICollectionViewCell的类
在.h中
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UILabel *label;

在.m中
@implementation MyCollectionViewCell
-(UIImageView *)imageView{
if (!_imageView) {
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 200, 180)];
}
return _imageView;
}

-(UILabel *)label{
if (!_label) {
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 180, 80, 20)];
}
return _label;
}

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

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值