二、通过纯代码创建的UICollectionView的简单使用

之前写了一个Xib创建UiCollectionView的简单使用的demo,现在写个用纯代码写的,原理其实差不多,只是那个Cell之前是用XIb现在用一个继承UiColelctionViewCell的类代替,如下所示:


1.首先创建一个CollectionCell的类继承UIColelctionViewCell

@interface CollectionCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) UILabel *label;

@end

里面的UIImageView和UILabel控件是我定义的加在cell上面的控件;

2.在OneViewController的.h文件中实现三个协议:如下所示,并声明UICollectionView控件

<span style="font-size:14px;">@interface OneViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>


@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSMutableArray *dataArray;

@end</span>


3.在OneViewController的.m文件中初始化UICollectionView控件

<span style="font-size:14px;">    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; //水平滚动
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 100) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;        //实现代理
    self.collectionView.dataSource = self;      //实现数据源方法
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    //注册cell,通过创建的CollectionCell
    [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CELL"];</span>



4.实现其相关的代理方法

<span style="font-size:14px;">#pragma mark UICollectionViewDelegateFlowLayout的代理方法
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(100, 100);
}

#pragma mark UICollectionViewDataSource的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    
    [cell.imageView setImage:[UIImage imageNamed:[self.dataArray objectAtIndex:indexPath.row]]];
    [cell.label setText:[self.dataArray objectAtIndex:indexPath.row]];
    cell.backgroundColor = [UIColor redColor];
    return cell;
    
}

#pragma mark UICollectionViewDelegate的代理方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionCell *cell = (CollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
}

-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionCell *cell = (CollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
}</span>


5.ok了运行完效果图如下:

可以左右滑动的;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值