码迷,mamicode.com
首页 > 其他好文 > 详细

关于UICollectionViewCell重用问题

时间:2015-09-17 15:01:58      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

 

懒人直接上代码

1.

    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];

    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    self.collView  =[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth,ScreenHeight-NAVIGATIONBAR_HEIGHT-TABBAR_HEIGHT) collectionViewLayout:flowLayout];

    self.collView.dataSource=self;

    self.collView.delegate=self;

    self.collView.alwaysBounceVertical = YES;

    //注册Cell,必须要有

    [self.collView registerClass:[LYDianPuGoodsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    self.collView.backgroundColor = [UIColor grayColor];

    [self.view addSubview: self.collView];

2.

#pragma mark -- UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数

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

{

    return self.dianpuArray.count;

}

 

//定义展示的Section的个数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 1;

}

 

//每个UICollectionView展示的内容

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

{

 

    LYDianPuGoodsCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor whiteColor];

    if(self.dianpuArray.count>0)

    {

        cell.labgoodsName.text=self.dianpuArray[indexPath.row];

    }

    return cell;

}

 

 

//定义每个Item 的大小

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

{

    return CGSizeMake((ScreenWidth-20)/2, (ScreenWidth-20)/2);

}

 

//定义每个UICollectionView 的 margin

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

{

    return UIEdgeInsetsMake(0, 5, 0, 5);

}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

{

    return 5;

}

-(CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    return 5;

    

}

#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法

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

{

    

    [self.collView deselectItemAtIndexPath:indexPath animated:YES];

    

}

 

//返回这个UICollectionView是否可以被选择

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}

3.

-(id)initWithFrame:(CGRect)frame{

    if(self==[super initWithFrame:frame])

    {

        self.labgoodsName=[[UILabel alloc]init];

        self.labgoodsName.font=[UIFont systemFontOfSize:14.0f];

        [self addSubview:self.labgoodsName];

    }

    return self;

}

-(void)layoutSubviews{

    self.labgoodsName.frame=CGRectMake((self.width-80)/2, self.height-30, 80, 20);

}

关于UICollectionViewCell重用问题

标签:

原文地址:http://www.cnblogs.com/pingandashen/p/4816366.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!