通过Xib创建 UICollectionView 和自定义UICollectionViewCell
2016-04-03 17:50
399 查看
1。在控制器的viewDidLoad方法中添加代码
CGFloat itemWidth = (kScreenW - kSpacingW * 3) / 2;
NSLog(@"itemWidth == %f",itemWidth);
CGFloat itemHeight = itemWidth * 0.75 + 71;
NSLog(@"itemWidth == %f",itemHeight);
UICollectionViewFlowLayout*layout = [[UICollectionViewFlowLayout alloc]init];
layout.itemSize = CGSizeMake(itemWidth, itemHeight);
layout.minimumInteritemSpacing = kSpacingW;
layout.minimumLineSpacing = kSpacingW;
layout.sectionInset = UIEdgeInsetsMake(0, kSpacingW, 0, kSpacingW);
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
[self.view addSubview:self.collectionView];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[HotCell class] forCellWithReuseIdentifier:@"HotCell"];
2.实现collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
HotCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
3.通过XIB自定义cell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 初始化时加载collectionCell.xib文件
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"HotCell" 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;
}
CGFloat itemWidth = (kScreenW - kSpacingW * 3) / 2;
NSLog(@"itemWidth == %f",itemWidth);
CGFloat itemHeight = itemWidth * 0.75 + 71;
NSLog(@"itemWidth == %f",itemHeight);
UICollectionViewFlowLayout*layout = [[UICollectionViewFlowLayout alloc]init];
layout.itemSize = CGSizeMake(itemWidth, itemHeight);
layout.minimumInteritemSpacing = kSpacingW;
layout.minimumLineSpacing = kSpacingW;
layout.sectionInset = UIEdgeInsetsMake(0, kSpacingW, 0, kSpacingW);
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
[self.view addSubview:self.collectionView];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[HotCell class] forCellWithReuseIdentifier:@"HotCell"];
2.实现collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
HotCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
3.通过XIB自定义cell
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 初始化时加载collectionCell.xib文件
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"HotCell" 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;
}
相关文章推荐
- 2016年腾讯校招在线笔试UI开发总结
- 01-复杂度2 Maximum Subsequence Sum
- AnimMontage(中文)——UE4官方文档翻译与理解
- 关于最小堆以及priority_queue
- UIGraphicsBeginImageContext和UIGraphicsBeginImageContextWithOptions实现iOS中的截图功能
- AQS(2):acquire
- 96. Unique Binary Search Trees
- 跳转到主界面“UIStoryboard”
- leetcode---Range Sum Query - Immutable---线段树
- lucene整理4 -- 各种Query
- 编译 uImage
- poj 1679 The Unique mst
- 远程服务器返回了意外相应:(413) Request Entity Too Large。
- sqm(sqlmapGUI) pcat修改版
- Android UI基础——ImageView&ImageButton控件
- Android学习笔记---第二天---基础UI组件---TextView
- poj--3061 Subsequence(前缀和 or 尺取法)
- service builder不会自动建表/更新字段
- JAVA逆向&反混淆-追查Burpsuite的破解原理(转)
- OpenStack : 使用 Fuel 部署私有云服务