UICollectionView

UICollectionView跟安卓中的RecycleView很像,使用也很像,安卓通过adapter,ios通过UICollectionViewDelegate,UICollectionViewDataSource协议


具体使用方式:

1.新建ios工程

2.ViewController中实现相关协议

#import <UIKit/UIKit.h>

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


@end


3.实现ViewController

//
//  ViewController.m
//  uicollectionview
//
//  Created by Young on 15/10/9.
//  Copyright (c) 2015年 Young. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

static NSString* cid=@"cid";

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //采用流布局,若想实现其他功能,比如按下抬起效果等可以实现UICollectionViewDelegateFlowLayout中相关协议
    UICollectionView* collec=[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];
    
    [collec registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cid];
    
    //设置自身为数据源
    collec.dataSource=self;
    
    collec.delegate=self;
    
    [self.view addSubview:collec];
    

    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return  CGSizeMake(120, 120);

}
//待显示的数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return 50;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
//UICollectionView中每一个控件是一个UICollectionViewCell,可以通过继承UICollectionViewCell来自定义布局
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:cid forIndexPath:indexPath];
    
    cell.backgroundColor=[UIColor yellowColor];
    return cell;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


效果图



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值