iOS UICollectionView手寫代碼實現步驟

來源:互聯網
上載者:User

iOS UICollectionView手寫代碼實現步驟
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);

}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.