您的位置:首页 > 产品设计 > UI/UE

iOS学习之UICollectionVuew基本使用

2015-12-16 13:40 555 查看
UICollectionView的使用和UITableView基本类似,UICollectionView也可以自定义UICollectionViewCell,至少需要实现两个方法

首先来看一下效果图:



新建项目CollectionView

在Main.storyboard中的ViweController的View上拉一个CollectionView并关联相应的协议

其次新建自定义UICollectionViewCell的class及xib文件

关键代码:

//
// ViewController.m
// CollectionView
//
// Created by 安前松 on 15/12/16.
// Copyright © 2015年 安前松. All rights reserved.
//

#import "ViewController.h"
#import "CollectionViewCell.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.nil
[self.collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
}

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

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 5;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell=[self.collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
if(cell==nil){
cell=[[[NSBundle mainBundle] loadNibNamed:@"CollectionViewCell" owner:self options:nil] lastObject];
}
cell.label.text=@"測試";
cell.imageView.image=[UIImage imageNamed:@"bird.png"];
return cell;
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: