文件结构:
一:先定义cell,这里是Cell类,继承自UICollectionViewCell,用xib画出cell
CollectionCell.h
1 #import <UIKit/UIKit.h> 2 3 @interface CollectionCell : UICollectionViewCell 4 5 @end
CollectionCell.c
1 #import "CollectionCell.h" 2 3 @implementation CollectionCell 4 5 - (id)initWithFrame:(CGRect)frame 6 { 7 self = [super initWithFrame:frame]; 8 if (self) 9 { 10 // 初始化时加载collectionCell.xib文件 11 NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil]; 12 13 // 如果路径不存在,return nil 14 if (arrayOfViews.count < 1) 15 { 16 return nil; 17 } 18 // 如果xib中view不属于UICollectionViewCell类,return nil 19 if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) 20 { 21 return nil; 22 } 23 // 加载nib 24 self = [arrayOfViews objectAtIndex:0]; 25 } 26 return self; 27 }
CollectionCell.xib
画出cell:
设置Cell大小:
设置Identifier:
设置Class:
二:UICollectionViewController
1.CollectionViewController.h
1 #import <UIKit/UIKit.h> 2 #import "CollectionCell.h" 3 4 @interface CollectionViewController : UICollectionViewController 5 6 7 @end
2.CollectionViewController.c
1 #import "CollectionViewController.h" 2 3 @interface CollectionViewController () 4 5 @end 6 7 @implementation CollectionViewController 8 9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 10 { 11 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 12 if (self) { 13 // Custom initialization 14 } 15 return self; 16 } 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 // Do any additional setup after loading the view from its nib. 22 23 self.collectionView.dataSource = self; 24 self.collectionView.delegate = self; 25 } 26 27 - (void)didReceiveMemoryWarning 28 { 29 [super didReceiveMemoryWarning]; 30 // Dispose of any resources that can be recreated. 31 } 32 33 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 34 { 35 return 4; 36 } 37 38 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 39 { 40 [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"]; 41 42 CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath]; 43 44 45 return cell; 46 } 47 48 //- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 49 //{ 50 // CGFloat width = indexPath.row * 50; 51 // CGFloat height = indexPath.row * 50; 52 // return CGSizeMake(width,height); 53 // 54 //} 55 56 @end
3.CollectionViewController.xib:
结构:
删除原来的view,拖一个collectionView进来
设置file‘s owner
custom class
cell大小及间隙
outlet:
常见错误:
1.只有collectionView,没有Cell显示出来,那么重写自定义Cell类的 - (id)initWithFrame:(CGRect)frame 方法,从nib加载或者自定义。
2.Xcode运行时报错:
UICollectionElementKindCell with identifier CollectionCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard
检查有没有注册identifier:在viewDidLoad方法里加入:
[self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];
3.
‘NSInternalInconsistencyException‘, reason: ‘-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "CollectionViewController" nib but the view outlet was not set.
file‘s owner中的view属性没有设置,连线view属性和xib中的collectionView
UICollectionView的使用,布布扣,bubuko.com
原文:http://www.cnblogs.com/hereiam/p/3848466.html