手写CollectionVeiwCell和UICollectionView

其中创建一个继承于UICollectionViewCell的类

点h中描述出上面想要添加的控件

#import <UIKit/UIKit.h>


@interface CollectionViewCell :UICollectionViewCell

@property(nonatomic,strong)UIImageView *CollimageView;

@property(nonatomic,strong)UILabel *Colllab;


类然后再点m中给它设定一下大小

#import "CollectionViewCell.h"


@implementation CollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame

{

    

   self=[superinitWithFrame:frame];

   if (self)

    {

        //设置CollimageView的大小和在CollCell自带的contentVeiw上面的位置

        self.CollimageView=[[UIImageViewalloc]initWithFrame:CGRectMake(0, 0, 120, 130)];

        //设置Colllabel的大小和所在的位置

        self.Colllab=[[UILabelalloc]initWithFrame:CGRectMake(20, 40, 100, 20)];

        

      //添加到Collection上面,其中self就是这个sell本身,因为CollimageColllab都是它自身的东西,所以要用self掉,但是又要加到CellcontentView上面

        

        [self.contentViewaddSubview:self.CollimageView];

        [self.contentViewaddSubview:self.Colllab];

    }

//返回一个cell

    return self;

}

@end


然后创建一个继承于 UIViewController

在点h文件中添加代理和数据源方法

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

Collectioncell与tableviewcell的区别在于它有三个代理方法
其中 UICollectionViewDelegateFlowLayout这个可以认为是 UICollectionViewDelegateFlowLayout   的子类

数据然后在点m文件中实现

#import "ViewController.h"

#import "CollectionViewCell.h"

@interface ViewController ()

{



   UICollectionView * _collectionView;


    NSMutableArray *MuArry;

   NSMutableArray *MuArry2;


}

@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    

    //给需要用的图片放到一个数组内部

    MuArry =[[NSMutableArrayalloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg",@"8.jpg",@"9.jpg",nil];

    

    MuArry2=[[NSMutableArrayalloc]initWithObjects:@"这是第几个",@"这是第几个",@"这是第几个",@"这是第几个",@"这是第几个",@"这是第几个",@"这是第几个",@"这是第几个",@"这是第几个",nil];

    

    

    //实现CollectionView的布局UICollectionViewFlowLayout也是代理的一部分

    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayoutalloc]init];

    

    //初始化_collectionView使用flowLayout

    _collectionView=[[UICollectionViewalloc]initWithFrame:CGRectMake(0, 50,self.view.bounds.size.width,self.view.bounds.size.height)collectionViewLayout:flowLayout];

    [_collectionViewsetBackgroundColor:[UIColorwhiteColor]];

    //设置代理

    _collectionView.delegate=self;

    //设置数据源

    _collectionView.dataSource=self;

    

    

    

    //collectionView添加到self.view上面

    [self.viewaddSubview:_collectionView];

}

//设定UICollectionview内部Cell的数量

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

{

    returnMuArry.count;

    

}

//设定每个cell的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

   return CGSizeMake(120, 130);

}

//设置每个Collcell距离另外一个Cell的距离

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

   return UIEdgeInsetsMake(10, 10, 10, 10);

}

//初始化cell重用

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    [_collectionViewregisterClass:[CollectionViewCellclass]forCellWithReuseIdentifier:@"CollCell"];

    CollectionViewCell *cell=[collectionViewdequeueReusableCellWithReuseIdentifier:@"CollCell"forIndexPath:indexPath];

    cell.CollimageView.image=[UIImageimageNamed:MuArry[indexPath.row]];

    cell.Colllab.text=MuArry2[indexPath.row];

    //cell.backgroundColor=[UIColor grayColor];

   return cell;


}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值