UICollectionView-数据源
2013-12-13 16:40
387 查看
1。<UICollectionViewDataSource> 数据源
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView //有多少个section,默认为1
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section //每个section内有多少项,即多少个cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath //cell内容
2。注册可复用的 cell
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier //注册 cellClass(可以是标准的cell,也可以是自定义的cell)
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier //注册.xib
e.g. UINib *nib = [UINib nibWithNibName: NSStringFromClass([MyCollectionViewCell class]) bundle:[NSBundle mainBundle]];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:kCollectionViewCellIdentifier];
@implementation MyCollectionViewCell
- (void) awakeFromNib{
[super awakeFromNib]; //load nib的时候做一些初始化工作
self.imageView.backgroundColor = [UIColor clearColor]; //必须先设置单元格内顶级层的view(如单元格内添加了imgView等)的背景色为clear,才能真正体现选中单元格的背景色
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
}
@end
e.g.
UICollectionViewController :
static NSString *kCollectionViewCellId = @"Cells";
- (instancetype) initWithCollectionViewLayout:(UICollectionViewLayout *)layout{
self = [super initWithCollectionViewLayout:layout];
if (self != nil){
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier :kCollectionViewCellId]; //注册可复用的cell,同UITableView,这里也可以是自定义的collectionViewCell
}
return self;
}
- (void) viewDidLoad{
[super viewDidLoad];
self.collectionView.backgroundColor = [UIColor whiteColor];
}
- (NSInteger)numberOfSectionsInCollectionView :(UICollectionView *)collectionView{
return [self arySectionsSource count ];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 3 + arc4random_uniform(5); // 随机数 arc4random_uniform(num) // [ 0 , num )
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
// indexPath : indexPath.section , indexPath.row
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier :kCollectionViewCellIdentifier forIndexPath:indexPath];
。。。。。//设置cell内容
return cell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView //有多少个section,默认为1
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section //每个section内有多少项,即多少个cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath //cell内容
2。注册可复用的 cell
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier //注册 cellClass(可以是标准的cell,也可以是自定义的cell)
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier //注册.xib
e.g. UINib *nib = [UINib nibWithNibName: NSStringFromClass([MyCollectionViewCell class]) bundle:[NSBundle mainBundle]];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:kCollectionViewCellIdentifier];
@implementation MyCollectionViewCell
- (void) awakeFromNib{
[super awakeFromNib]; //load nib的时候做一些初始化工作
self.imageView.backgroundColor = [UIColor clearColor]; //必须先设置单元格内顶级层的view(如单元格内添加了imgView等)的背景色为clear,才能真正体现选中单元格的背景色
self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
}
@end
e.g.
UICollectionViewController :
static NSString *kCollectionViewCellId = @"Cells";
- (instancetype) initWithCollectionViewLayout:(UICollectionViewLayout *)layout{
self = [super initWithCollectionViewLayout:layout];
if (self != nil){
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier :kCollectionViewCellId]; //注册可复用的cell,同UITableView,这里也可以是自定义的collectionViewCell
}
return self;
}
- (void) viewDidLoad{
[super viewDidLoad];
self.collectionView.backgroundColor = [UIColor whiteColor];
}
- (NSInteger)numberOfSectionsInCollectionView :(UICollectionView *)collectionView{
return [self arySectionsSource count ];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 3 + arc4random_uniform(5); // 随机数 arc4random_uniform(num) // [ 0 , num )
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
// indexPath : indexPath.section , indexPath.row
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier :kCollectionViewCellIdentifier forIndexPath:indexPath];
。。。。。//设置cell内容
return cell;
}
相关文章推荐
- 关于 UICollectionView 的一些代理和数据源方法
- UICollectionView使用过程中的常见错误
- UICollectionviewCell自适应label长度
- iOS UI-集合视图(UICollectionView)
- iOS UIcollectionview 数据量少时候bounces无效的解决方法
- ios UICollectionViewController集合视图(三)
- 用UICollectionView实现无限轮播案列
- UICollectionView 基础全面解析
- UICollectionView详解
- UICollectionView如果在数据不够一屏时上下滚动
- UICollectionView cellForItemAtIndexPath 方法不走
- ios uicollectionview滚动速度/灵敏度调节
- Objective-C-UI控件学习之UICollectionViewFlowLayout
- ios 报表 表格 UICollectionViewLayout
- UIcollectionView的使用(首页的搭建4)
- 蓝懿IOS学习UICollectionView实战轮播图
- 用UIScrollView实现UICollectionView显示
- UICollectionView 的设定关于UICollectionReusableView一种设置(在UICollectionView页面中)
- iOS 8自动调整UITableView和UICollectionView布局
- iOS: 玩转UICollectionViewLayout