UICollectionView-数据源
来源:互联网 发布:广联达电力计价软件 编辑:程序博客网 时间:2024/04/24 16:12
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;
}
- UICollectionView-数据源
- 关于 UICollectionView 的一些代理和数据源方法
- UICollectionView--UICollectionViewController
- UICollectionView-布局
- UICollectionView-事件
- iOS-UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- CVS on Win7
- 开发笔记:解决安卓GestureOverlayView手势和ListView点击事件、文本框获取焦点冲突的问题
- ZOJ-1292
- Maximo学习笔记------关联字段显示详细描述
- Sequence 优先队列模拟堆 poj 2442
- UICollectionView-数据源
- quick-cocos2dx AppBase
- [推荐]AlloVince 的 VIM 配置方案
- 腾讯风铃怎么连接数据库的呢
- ZOJ-1331
- SqlServer中,用exec处理sql字符串中含有变量,提示:必须声明标量变量@(已解决!)
- SeekBar OpenGLRenderer GL_INVALID_VALUE
- 确保测试代码不会在发布版上运行
- UIWebView让你看得不爽?