首页 > 代码库 > UICollectionView的使用
UICollectionView的使用
文件结构:
一:先定义cell,这里是Cell类,继承自UICollectionViewCell,用xib画出cell
CollectionCell.h
1 #import <UIKit/UIKit.h>2 3 @interface CollectionCell : UICollectionViewCell4 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 nil14 if (arrayOfViews.count < 1)15 {16 return nil;17 }18 // 如果xib中view不属于UICollectionViewCell类,return nil19 if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])20 {21 return nil;22 }23 // 加载nib24 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 : UICollectionViewController5 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 *)nibBundleOrNil10 {11 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];12 if (self) {13 // Custom initialization14 }15 return self;16 }17 18 - (void)viewDidLoad19 {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)didReceiveMemoryWarning28 {29 [super didReceiveMemoryWarning];30 // Dispose of any resources that can be recreated.31 }32 33 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section34 {35 return 4;36 }37 38 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath39 {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 *)indexPath49 //{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