用CollectionView做一个SrollView

UICollectionView类似于UItableView实现方法也类似

1、先创建一个工程名字叫做CollectionView

2、新建另外一个名为CollectionViewCell的文件同时生成Xib文件继承自UICollectionViewCell;

3、在Xib文件中得CollectionViewCell中添加一个ImageView与一个Label;

4、将imageVi以及Label关联到UICollectionCell中,分别叫做image以及label;;

5、在Main.storyboard中删除原有的View,拖一个CollectionView进来;

6、将delegate以及dataSource关联ViewController上面  记住 不要关联到view上面;

7、选中CollectionViewCell的属性中得Class修改为 CollectionViewCell;

7、选中CollectionView的属性,可以修改其属性,比如是垂直滑动,还是水平滑动,选择Vertical或Horizontal;

8、在CollectionViewCell.m里面初始化CollectionCell.xib文件;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionViewCell" owner:self options:nil];
        
        // 如果路径不存在,return nil
        if (arrayOfViews.count < 1)
        {
            return nil;
        }
        // 如果xib中view不属于UICollectionViewCell类,return nil
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])
        {
            return nil;
        }
        // 加载nib
        self = [arrayOfViews objectAtIndex:0];
    }
    return self;
}


9、在ViewController.h里面声明UICollectionViewDelegate,UICollectionViewDataSource;

10、在ViewDidLoad方法中注册CollectionViewCell的类,在ViewDidLoad方法中添加,不注册,将无法加载,程序崩溃其中,CollectionCell是这个Cell的标识;

[self.CollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];

11、在ViewController.m里面创建Cell 并且可以给Cell的属性image以及label添加数据

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *Cell = (CollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
    Cell.image.image = [UIImage imageNamed:@"appIcon57.png"];
    Cell.label.text = @"ddd";
    Cell.label.textColor = [UIColor blueColor];
    Cell.backgroundColor = [UIColor redColor];
    
    return Cell;
}

12控制每个section的item个数

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 150;
}
113控制section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;

}
14、控制UICollectionVieCell的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(96, 100);
}
15、返回这个UICollectionViewUCell能否被返回
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
总结:

关于CollectionView的创建和tabeleVIew差不多,只要注意一些细节比如关联代码的时候一定要关联争取的NIB;Cell一定要初始化注册不然程序会崩溃;

利用XIb文件时要注意修改identifier以及Class;

Xib文件一定要初始化;









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值