IOS UICollectionView handwritten code implementation steps
IOS UICollectionView handwritten code implementation steps
//
// ViewController. h
// CollectionView handwritten code
//
// Created by yangxiuying on 14/11/28.
// Copyright (c) 2014 lanjiying. All rights reserved.
//
# Import
@ Interface ViewController: UIViewController
{
NSString * identifier;
}
//
// ViewController. m
// CollectionView handwritten code
//
// Created by yangxiuying on 14/11/28.
// Copyright (c) 2014 lanjiying. All rights reserved.
//
# Import "ViewController. h"
@ Interface ViewController ()
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Identifier = @ "cell ";
// Initialize layout
UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
[FlowLayout setScrollDirection: UICollectionViewScrollDirectionVertical];
// UIEdgeInsets top = {15, 10, 15, 5 };
// [FlowLayout setSectionInset: top];
UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame: CGRectMake (0, 30,320,480) collectionViewLayout: flowLayout];
// Register a cell
[CollectionView registerClass: [UICollectionViewCell class] forCellWithReuseIdentifier: identifier];
// Set proxy
CollectionView. delegate = self;
CollectionView. dataSource = self;
[Self. view addSubview: collectionView];
}
# Pragma mark-collectionView delegate
// Set partitions
-(NSInteger) numberOfSectionsInCollectionView :( UICollectionView *) collectionView
{
Return 1;
}
// Set the element size box
-(UIEdgeInsets) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout insetForSectionAtIndex :( NSInteger) section
{
UIEdgeInsets top = {30, 15, 30, 15 };
Return top;
}
// Number of elements on each partition
-(NSInteger) collectionView :( UICollectionView *) collectionView numberOfItemsInSection :( NSInteger) section
{
Return 24;
}
// Set the Element Content
-(UICollectionViewCell *) collectionView :( UICollectionView *) collectionView cellForItemAtIndexPath :( NSIndexPath *) indexPath
{
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier: identifier forIndexPath: indexPath];
[Cell sizeToFit];
Cell. backgroundColor = [UIColor greenColor];
Return cell;
}
// Set the cell width
// Set the element size
-(CGSize) collectionView :( UICollectionView *) collectionView layout :( UICollectionViewLayout *) collectionViewLayout sizeForItemAtIndexPath :( NSIndexPath *) indexPath {
Return CGSizeMake (140,160 );
}