您的位置:首页 > 产品设计 > UI/UE

第04天实战技术(10):UICollectionView基本使用

2017-03-29 00:00 423 查看
#####一、UICollectionView基本使用

// 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


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息