IOS 手写UICollectionView

遇到第一个问题:

手写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:forCellWithReuseIdentifier:

    //    - (void)registerClass:forSupplementaryViewOfKind:withReuseIdentifier:

    //    - (void)registerNib:forCellWithReuseIdentifier:

    //    - (void)registerNib:forSupplementaryViewOfKind: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
本文原创,转载请注明出处: http://blog.csdn.net/zhenggaoxing/article/details/44488851


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值