UICollectionView

来源:互联网 发布:python汉化 编辑:程序博客网 时间:2024/05/19 02:44

UICollectionView跟安卓中的RecycleView很像,使用也很像,安卓通过adapter,ios通过UICollectionViewDelegate,UICollectionViewDataSource协议


具体使用方式:

1.新建ios工程

2.ViewController中实现相关协议

#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>@end

3.实现ViewController

////  ViewController.m//  uicollectionview////  Created by Young on 15/10/9.//  Copyright (c) 2015年 Young. All rights reserved.//#import "ViewController.h"@interface ViewController ()@endstatic NSString* cid=@"cid";@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    //采用流布局,若想实现其他功能,比如按下抬起效果等可以实现UICollectionViewDelegateFlowLayout中相关协议    UICollectionView* collec=[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];        [collec registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cid];        //设置自身为数据源    collec.dataSource=self;        collec.delegate=self;        [self.view addSubview:collec];        }- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{    return  CGSizeMake(120, 120);}//待显示的数量- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{            return 50;}// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath://UICollectionView中每一个控件是一个UICollectionViewCell,可以通过继承UICollectionViewCell来自定义布局- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{        UICollectionViewCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:cid forIndexPath:indexPath];        cell.backgroundColor=[UIColor yellowColor];    return cell;    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


效果图



0 0