IOS 手寫UICollectionView

來源:互聯網
上載者:User

IOS 手寫UICollectionView
遇到第一個問題:

手寫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
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.