遇到第一个问题:
手写UICollectionView 出错了,问题是:
UICollectionView must be initialized with a non-nil layout parameter
翻译一下:集合视图必须使用布局参数初始化。
分析一下代码:
<pre name="code" class="objc"> 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 ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@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