CollectionView

瀑布流, 和tableview类似,但可以一行铺多个

MyCell.h

#import <UIKit/UIKit.h>

@interface MyCell : UICollectionViewCell

@property(nonatomic,retain)UIImageView *picImageView;
@property(nonatomic,retain)UILabel *label;

@end

MyCell.m

#import "MyCell.h"

@implementation MyCell

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

-(void)createView{
    self.label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height/3)];
    [self addSubview:self.label];

    self.picImageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, self.frame.size.height/3, self.frame.size.width, self.frame.size.height/3*2)];
    [self addSubview:self.picImageView];

}

MyReusableView.h

#import <UIKit/UIKit.h>

@interface MyReusableView : UICollectionReusableView

@property(nonatomic,retain)UIView *myView;


@end

MyReusableView.m

#import "MyReusableView.h"

@implementation MyReusableView

-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self) {
        self.myView=[[UIView alloc] init];
        self.myView.backgroundColor=[UIColor yellowColor];
        [self addSubview:self.myView];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.myView.frame=CGRectMake(0, 0, 100, 100);
}



@end

ViewController.m

#import "ViewController.h"
#import "MyCell.h"
#import "UIImageView+WebCache.h"
#import "MyReusableView.h"

@interface ViewController ()<UICollectionViewDataSource,UIScrollViewDelegate>

@property(nonatomic,retain)NSMutableArray *arr;

@end

@implementation ViewController

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

    //官方提供的一种瀑布流效果
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    //这里对每一个用来显示的区域成为item,就是tableview上的cell
    //设置一下item的尺寸
    flowLayout.itemSize=CGSizeMake(100, 120);
    //通过flowLayout来设置样式
    //设置最小行间距
    flowLayout.minimumLineSpacing=2;
    flowLayout.minimumInteritemSpacing=2;
    //边框
    flowLayout.sectionInset=UIEdgeInsetsMake(20, 20, 20, 20);

    //设置flowlayout头部滚动范围
    flowLayout.headerReferenceSize=CGSizeMake(0, 300);

    //设置一下滑动方向
    //UICollectionViewScrollDirectionHorizontal水平滑动
//    flowLayout.scrollDirection=UICollectionViewScrollDirectionHorizontal;


    UICollectionView *collestionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    [self.view addSubview:collestionView];
    //设置代理人
    collestionView.delegate=self;
    collestionView.dataSource=self;

    [collestionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"reuse"];

    //注册头部或尾部的区域
    //参数一:需要注册的类型是UICollectionReusableView
    //参数二:指定注册视图的类型,是头还是尾视图
    //参数三:重用标志
    [collestionView registerClass:[MyReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];

    [self createData];


}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    MyReusableView *view=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"headerView" forIndexPath:indexPath];
    view.backgroundColor=[UIColor orangeColor];
    return view;
}


-(void)createData{
    NSString *path=[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"json"];
    NSData *data=[NSData dataWithContentsOfFile:path];
    self.arr=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSLog(@"%@",self.arr);
}


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

    return self.arr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor=[UIColor cyanColor];
    cell.label.text=[NSString stringWithFormat:@"%ld",indexPath.row];

    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.arr[indexPath.row][@"thumbURL"]]];


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值