第04天实战技术(10):UICollectionView基本使用
2017-03-29 00:00
423 查看
#####一、UICollectionView基本使用
-1.新手使用collectionView的几个注意点
1.1 创建一个UIcollectionView 必须要有一个布局参数
1.2 cell必须通过注册
1.3 cell必须要自定义
[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PhotoCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 1.选取缓存池去取
// 以后cell 有 forIndexPath 必须要通过注册、或者搜获
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
// 2.cell展示数据
NSString *imageName = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
cell.image = [UIImage imageNamed:imageName];
return cell;
}
#import "ViewController.h"
#import "PhotoCell.h"
@interface ViewController ()<UICollectionViewDataSource>
@end
@implementation ViewController
static NSString *const ID = @"cell"; // 不能让其他人瞎改 必须加上一个 const
(void)viewDidLoad {
[super viewDidLoad];
// UICollectionView 使用注意点
// 1.创建一个UIcollectionView 必须要有一个布局参数
// UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
// 2.cell必须通过注册
// 3.cell必须要自定义
#warning 问题
/**
问题1.
UICollectionView *collectionView = [[UICollectionView alloc]init];
>>
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
初始化的时候 不能给一个 nil layout 空的布局
1.解决办法
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:<#(CGRect)#> collectionViewLayout:<#(nonnull UICollectionViewLayout *)#>];
问题2.
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
>>
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
UICollectionViewCell 不能直接去缓存池去取,必须要注册
2.解决办法
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
}
#pragma mark UICollectionViewDataSource
(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 1.选取缓存池去取
// 以后cell 有 forIndexPath 必须要通过注册、或者搜获
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
NSString *imageName = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
cell.image = [UIImage imageNamed:imageName];
// 2.cell有可能为空
// if (cell == nil) {
// cell = [[UICollectionViewCell alloc]init];
// }
// cell.backgroundColor = [UIColor redColor];
return cell;
}
@end
#import <UIKit/UIKit.h>
@interface PhotoCell : UICollectionViewCell
@property(nonatomic,strong) UIImage *image;
@end
#import "PhotoCell.h"
@interface PhotoCell()
@property (weak, nonatomic) IBOutlet UIImageView *img_photoView;
@end
@implementation PhotoCell
(void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
(void)setImage:(UIImage *)image
{
_image = image;
_img_photoView.image = image;
}
@end
// UICollectionView 使用注意点 // 1.创建一个UIcollectionView 必须要有一个布局参数 // 2.cell必须通过注册 // 3.cell必须要自定义
-1.新手使用collectionView的几个注意点
1.1 创建一个UIcollectionView 必须要有一个布局参数
问题1.如果是直接UICollectionView *collectionView = [[UICollectionView alloc]init];进行初始化会出现下面的错误信息 报错>>> Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' 初始化的时候 不能给一个 nil layout 空的布局 1.解决办法 UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:<#(CGRect)#> collectionViewLayout:<#(nonnull UICollectionViewLayout *)#>];
1.2 cell必须通过注册
问题2.如果是直接从缓存池去取 UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; // 2.cell有可能为空 if (cell == nil) { cell = [[UICollectionViewCell alloc]init]; } 报错>>> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' UICollectionViewCell 不能直接去缓存池去取,必须要注册 2.解决办法 static NSString *const ID = @"cell"; // 不能让其他人瞎改 必须加上一个 const [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
1.3 cell必须要自定义
[collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PhotoCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 1.选取缓存池去取
// 以后cell 有 forIndexPath 必须要通过注册、或者搜获
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
// 2.cell展示数据
NSString *imageName = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
cell.image = [UIImage imageNamed:imageName];
return cell;
}
--- code ViewController
#import "ViewController.h"
#import "PhotoCell.h"
@interface ViewController ()<UICollectionViewDataSource>
@end
@implementation ViewController
static NSString *const ID = @"cell"; // 不能让其他人瞎改 必须加上一个 const
(void)viewDidLoad {
[super viewDidLoad];
// UICollectionView 使用注意点
// 1.创建一个UIcollectionView 必须要有一个布局参数
// UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
// 2.cell必须通过注册
// 3.cell必须要自定义
#warning 问题
/**
问题1.
UICollectionView *collectionView = [[UICollectionView alloc]init];
>>
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
初始化的时候 不能给一个 nil layout 空的布局
1.解决办法
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:<#(CGRect)#> collectionViewLayout:<#(nonnull UICollectionViewLayout *)#>];
问题2.
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
>>
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
UICollectionViewCell 不能直接去缓存池去取,必须要注册
2.解决办法
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
*/ // 流水布局 (比较窄,item 会慢慢流下去 , 宽了, item 就慢慢流上去) UICollectionViewFlowLayout UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init]; UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout]; collectionView.center =self.view.center; collectionView.backgroundColor = [UIColor orangeColor]; collectionView.bounds = CGRectMake(0, 0, self.view.bounds.size.width, 200); [self.view addSubview:collectionView]; // 2.设置数据源方法 collectionView.dataSource = self; // 3.注册cell //[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID]; [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PhotoCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
}
#pragma mark UICollectionViewDataSource
(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 1.选取缓存池去取
// 以后cell 有 forIndexPath 必须要通过注册、或者搜获
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
NSString *imageName = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
cell.image = [UIImage imageNamed:imageName];
// 2.cell有可能为空
// if (cell == nil) {
// cell = [[UICollectionViewCell alloc]init];
// }
// cell.backgroundColor = [UIColor redColor];
return cell;
}
@end
PhotoCell
#import <UIKit/UIKit.h>
@interface PhotoCell : UICollectionViewCell
@property(nonatomic,strong) UIImage *image;
@end
#import "PhotoCell.h"
@interface PhotoCell()
@property (weak, nonatomic) IBOutlet UIImageView *img_photoView;
@end
@implementation PhotoCell
(void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
(void)setImage:(UIImage *)image
{
_image = image;
_img_photoView.image = image;
}
@end
相关文章推荐
- 第04天实战技术(11):UICollectionView基本使用(布局)
- 第04天实战技术(12):UICollectionView基本使用(抽取方式1)
- 第04天实战技术(13):UICollectionView基本使用(抽取方式2)
- 第04天实战技术(15):UICollectionView计算布局尺寸
- 第04天实战技术(14):UICollectionView自定义布局方法
- 第02天实战技术(10):static和extern使用
- iOS_21团购_UICollectionView的基本使用
- 第04天实战技术(01):block开发使用场景(保存代码)
- iOS UICollectionViewController基本使用方法和简单的相册实现
- 第04天实战技术(09):block开发中使用场景(返回值)
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本概念与使用
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
- 第04天实战技术(03):block开发使用场景(传值)
- Android 技术之RecyclerView的基本使用(再不学会你就out了)
- iOS 心得二 UICollectionView的基本使用
- UICollectionView 集合视图基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用