你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS 手寫UICollectionView

IOS 手寫UICollectionView

編輯:IOS開發綜合

遇到第一個問題:

手寫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: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 ()

@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
 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved