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
首先来看一下效果图:
新建项目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
相关文章推荐
- 我对UI自动化测试的理解[转]
- iOS控件——UITableView详解
- iOS开发~interface Builder(简称 IB) 界面构建器
- OpenWRT开发之——BuildPackage剖析
- Win7+VS2010环境下CEGUI 0.8.4编译过程详解
- Android UiAutomator编译与运行测试代码
- Educational Codeforces Round 2 B. Queries about less or equal elements
- Quicksort
- UIViewController的生命周期
- UINavigationBar Background Color
- 关于UI布局中的常见布局类型、控件、控件属性以及引入布局的说明
- UICollectionView的使用
- iOS开发~视图(UIView)与控件(UIControl)
- UIPageViewController-浅析
- EASYUI COMBOBOX 自动补齐功能
- UItable配合数组的简单用法
- SQLiteDatabase中query、insert、update、delete方法参数说明
- Android UiAutomator第一个测试工程
- sonarquer搭建问题
- easyui如何动态显示、隐藏列