UICollectionView的简单使用
2013-12-20 09:29
411 查看
转自:http://my.oschina.net/joanfen/blog/160156
iOS CollectionView的出现是一大福利,再也不用用TableView来定义复杂的多栏表格了,用法与Table类似,只是Cell必须自己添加,无默认模式
由于CollectionView没有默认的Cell布局,所以一般还是自定义方便又快捷
一、自定义Cell
1、新建类CollectionCell继承自UICollectionViewCell
2、新建Xib,命名为CollectionCell.xib
a.选中CollectionCell.xib删掉默认的View,从控件中拖一个Collection View Cell(图3)到画布中,设置大小为95*116;
b.选中刚刚添加的Cell,更改类名为CollectionCell,如图4
c.在CollectionCell.xib的CollectionCell中添加一个ImageView和一个Label(图5)
d.创建映射, 图6,图7
e.选中CollectionCell.m , 重写init方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 在此添加
// 初始化时加载collectionCell.xib文件
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" 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;
}
二、定义UICollectionView;
1、拖动一个Collection View到指定ViewController的View上
2、连线dataSource和delegate,并创建映射,命名为CollectionView
3、选中CollectionView的标尺,将Cell Size的Width和Height改成与自定义的Cell一样的95*116,图8
4、选中CollectionView的属性,可以修改其属性,比如是垂直滑动,还是水平滑动,选择Vertical或Horizontal
5、在ViewDidLoad方法中声明Cell的类,在ViewDidLoad方法中添加,此句不声明,将无法加载,程序崩溃
// KCellID为宏定义 @"CollectionCell"
[self.collection registerClass:[CollectionCell class] forCellWithReuseIdentifier:KCellID];
6、在ViewController.h中声明代理
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
7、在.m文件中实现代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// 每个Section的item个数
return 12;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:KCellID forIndexPath:indexPath];
// 图片的名称
NSString *imageToLoad = [NSString stringWithFormat:@"%d.JPG", indexPath.row];
// 设置label文字
cell.label.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.row,(long)indexPath.section];
// 设置imageView的图片
cell.imageView.image = [UIImage imageNamed:imageToLoad];
return cell;
}
效果如图10
点击某项后跳转事件与UITableView类似,实现代理方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
即可,不赘述
iOS CollectionView的出现是一大福利,再也不用用TableView来定义复杂的多栏表格了,用法与Table类似,只是Cell必须自己添加,无默认模式
由于CollectionView没有默认的Cell布局,所以一般还是自定义方便又快捷
一、自定义Cell
1、新建类CollectionCell继承自UICollectionViewCell
2、新建Xib,命名为CollectionCell.xib
a.选中CollectionCell.xib删掉默认的View,从控件中拖一个Collection View Cell(图3)到画布中,设置大小为95*116;
b.选中刚刚添加的Cell,更改类名为CollectionCell,如图4
c.在CollectionCell.xib的CollectionCell中添加一个ImageView和一个Label(图5)
d.创建映射, 图6,图7
e.选中CollectionCell.m , 重写init方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 在此添加
// 初始化时加载collectionCell.xib文件
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" 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;
}
二、定义UICollectionView;
1、拖动一个Collection View到指定ViewController的View上
2、连线dataSource和delegate,并创建映射,命名为CollectionView
3、选中CollectionView的标尺,将Cell Size的Width和Height改成与自定义的Cell一样的95*116,图8
4、选中CollectionView的属性,可以修改其属性,比如是垂直滑动,还是水平滑动,选择Vertical或Horizontal
5、在ViewDidLoad方法中声明Cell的类,在ViewDidLoad方法中添加,此句不声明,将无法加载,程序崩溃
// KCellID为宏定义 @"CollectionCell"
[self.collection registerClass:[CollectionCell class] forCellWithReuseIdentifier:KCellID];
6、在ViewController.h中声明代理
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
7、在.m文件中实现代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// 每个Section的item个数
return 12;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:KCellID forIndexPath:indexPath];
// 图片的名称
NSString *imageToLoad = [NSString stringWithFormat:@"%d.JPG", indexPath.row];
// 设置label文字
cell.label.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.row,(long)indexPath.section];
// 设置imageView的图片
cell.imageView.image = [UIImage imageNamed:imageToLoad];
return cell;
}
效果如图10
点击某项后跳转事件与UITableView类似,实现代理方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
即可,不赘述
相关文章推荐
- iOS UICollectionViewController基本使用方法和简单的相册实现
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- UICollectionView的简单使用
- iOS流水布局UICollectionView简单使用
- UICollectionView的简单使用和常用代理方法
- UICollectionView的简单使用(二)— 瀑布流(石工布局)
- Swift UICollectionView 简单使用
- UICollectionView简单使用
- UICollectionView的简单使用
- 使用UICollectionView和UITableView混合布局,完成简单天气demo
- IOS基础_ UICollectionView的简单使用
- UICollectionView学习<1>之UICollectionView的简单使用
- 二、通过纯代码创建的UICollectionView的简单使用
- UICollectionViewLayout的简单使用(简单瀑布流)
- UICollectionView 简单使用详解
- UICollectionView 简单使用
- iOS UICollectionView简单使用
- iOS开发笔记之UICollectionView的简单使用
- iOS学习9:UICollectionView的简单使用
- UICollectionVIew的简单使用