UICollectionView基本使用
学习iOS一段时间了,早听说了UICollectionView的强大一直没有机会使用,今天自己研究了一下。
UICollectonView类似UITableView 用法基本相似, UICollectionView 更灵活,更强大
UICollectonView 将其子视图的位置,大小和外观的控制权委托给一个单独的布局对象。通过提供一个自定义布局对象,你几乎可以实现任何你能想象到得布局。
下面是UICollectionView 常用的代理方法。
//定义展示的UICollectionViewCell的个数 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 100; } //定义展示的Section的个数 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString * CellIdentifier = @"CollectionCell"; CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.imageView.image = [UIImage imageNamed:@"1680_1050_3983.jpg"]; // [cell sizeToFit]; return cell; } #pragma mark --UICollectionViewDelegateFlowLayout //定义每个UICollectionView 的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(110, 120); } //定义每个UICollectionView 的 margin -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(10, 10, 10, 10); } #pragma mark --UICollectionViewDelegate //UICollectionView被选中时调用的方法 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; } //返回这个UICollectionView是否可以被选择 -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { return YES; }
下面给出一个实例
首先自定义一个UICollectionViewCell
#import <UIKit/UIKit.h> @interface CollectionViewCell : UICollectionViewCell @property (nonatomic, strong) UIImageView *imageView; @property (nonatomic, strong) UILabel *titleLabel; @end #import "CollectionViewCell.h" @interface CollectionViewCell() @end @implementation CollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor purpleColor]; [self createView]; } return self; } - (UIImageView *)imageView { if (_imageView == nil) { CGRect frame = CGRectMake(10, 10, 80, 80); _imageView = [[UIImageView alloc] initWithFrame:frame]; } return _imageView; } - (UILabel *)titleLabel { if (_titleLabel == nil) { CGRect frame = CGRectMake(10, 100, 100, 20); _titleLabel = [[UILabel alloc] initWithFrame:frame]; _titleLabel.textAlignment = NSTextAlignmentCenter; } return _titleLabel; } - (void)createView { [self.contentView addSubview:self.imageView]; [self.contentView addSubview:self.titleLabel]; } @end
在控制器里设置代理
UICollectionView 初始化是一定要指定布局对象
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end // // ViewController.m // CollectionViewController // // Created by chao on 15/8/3. // Copyright (c) 2015年 chao. All rights reserved. // #import "ViewController.h" #import "CollectionViewCell.h" @interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation ViewController - (UICollectionView *)collectionView { if (_collectionView == nil) { CGRect frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; _collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout]; _collectionView.dataSource = self; _collectionView.delegate = self; } return _collectionView; } - (void)viewDidLoad { [super viewDidLoad]; [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"]; [self.view addSubview:self.collectionView]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //定义展示的UICollectionViewCell的个数 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 100; } //定义展示的Section的个数 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } //每个UICollectionView展示的内容 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString * CellIdentifier = @"CollectionCell"; CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.imageView.image = [UIImage imageNamed:@"1680_1050_3983.jpg"]; cell.titleLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row]; // [cell sizeToFit]; return cell; } #pragma mark --UICollectionViewDelegateFlowLayout //定义每个UICollectionView 的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(110, 120); } //定义每个UICollectionView 的 margin -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(10, 10, 10, 10); } #pragma mark --UICollectionViewDelegate //UICollectionView被选中时调用的方法 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; cell.backgroundColor = [UIColor whiteColor]; } //返回这个UICollectionView是否可以被选择 -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } @end
// // AppDelegate.h // CollectionViewController // // Created by chao on 15/8/3. // Copyright (c) 2015年 chao. All rights reserved. // #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController *vc = [[ViewController alloc] init]; self.window.rootViewController = vc; [self.window makeKeyAndVisible]; return YES; } @end