文件結構:
一:先定義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
3.出現錯誤:'UICollectionView must be initialized with a non-nil layout parameter'
去xib文件中的collectionView下面的Collection View Flow Layout中的sizeInspector中改變一下Min Spacing或者給在Cell的xib中設置一下Cell的IDentiFier