你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UICollectionView手寫代碼實現步驟

iOS UICollectionView手寫代碼實現步驟

編輯:IOS開發綜合
iOS UICollectionView手寫代碼實現步驟

//

// ViewController.h

// collectionView手寫代碼

//

// Created by yangxiuying on 14/11/28.

// Copyright (c) 2014年 lanjiying. All rights reserved.

//


#import


@interface ViewController : UIViewController

{

NSString * identifier;

}

//

// ViewController.m

// collectionView手寫代碼

//

// 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";

// 初始化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];

//注冊單元格

[collectionView registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:identifier];

//設置代理

collectionView.delegate = self;

collectionView.dataSource = self;

[self.view addSubview:collectionView];

}

#pragma mark - collectionView delegate

//設置分區



-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

return 1;

}

//設置元素的的大小框

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

UIEdgeInsets top = {30,15,30,15};

return top;

}

//每個分區上得元素個數

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

return 24;

}

//設置元素內容

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

UICollectionViewCell * cell =[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

[cell sizeToFit];

cell.backgroundColor =[UIColor greenColor];

return cell;

}

//設置單元格寬度

//設置元素大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

return CGSizeMake(140,160);

}


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