手寫UICollectionView 出錯了,問題是:
UICollectionView must be initialized with a non-nil layout parameter
翻譯一下:集合視圖必須使用布局參數初始化。
UICollectionView *first=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; [self.view addSubview:first];
很顯然啊,我沒有使用布局對象,現在稍作修改UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc ]init]; UICollectionView *first=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 320) collectionViewLayout:flowLayout]; [self.view addSubview:first];
frame
The frame rectangle for the collection view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This frame is passed to the superclass during initialization.
layout
The layout object to use for organizing items. The collection view stores a strong reference to the specified object. Must not be nil.
Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UICollectionView.m:3318
因為我平時都配合storyboard寫代碼,所以手寫的時候會出現這個問題。因為,storyboard裡面我設置了復用標識符,和cell的類。這裡需要我用代碼完成這個操作,所以會出錯。其實很簡單的,一行代碼:
1)
必須使用下面的方法進行Cell類的注冊:
// - (void)registerClass:forCellWithReuseIdentifi er:
// - (void)registerClass:forSupplementaryViewOfKi nd:withReuseIdentifier:
// - (void)registerNib:forCellWithReuseIdentifi er:
// - (void)registerNib:forSupplementaryViewOfKi nd:withReuseIdentifier:
//初始化
// // ViewController.m // 手寫UICollectionView // // Created by Bc_Ltf on 15/3/20. // Copyright (c) 2015年 Bc_Ltf. All rights reserved. // #import ViewController.h #define WEIGHT [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height @interface ViewController ()@end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self setup]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark- setup -(void)setup{ UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc ]init]; UICollectionView *first=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,WEIGHT ,HEIGHT) collectionViewLayout:flowLayout]; /* * 注冊cell */
//本文原創,轉載請注明出處:http://blog.csdn.net/zhenggaoxing/article/details/44488851 [first registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:@cell]; [self.view addSubview:first]; first.dataSource=self; first.delegate=self; } #pragma mark- Source Delegate -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 3; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 2; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *identify=@cell; // UICollectionViewCell *cell=[[UICollectionViewCell alloc] init]; UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath]; cell.contentView.backgroundColor=[UIColor whiteColor]; return cell; } #pragma mark- FlowDelegate - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(WEIGHT/3, WEIGHT/3); } -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return WEIGHT/9; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section; { return UIEdgeInsetsMake(WEIGHT/18, WEIGHT/9, WEIGHT/18, WEIGHT/9); } @end