关于UICollectionViewCell重用问题

 

懒人直接上代码

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);

}

posted @ 2015-09-17 14:40  赵平安  阅读(1302)  评论(0编辑  收藏  举报