CollectionView的使用
来源:互联网 发布:怎样下载qq软件 编辑:程序博客网 时间:2024/06/03 09:26
ColletionView(集合视图)是使用一个灵活的和可改变的布局来呈现一个有序的数据项集的方式。ColletionView最常见的用途是在网格状排列,但是在iOS集合视图可不仅仅是行和列。集合视图,精确的布局的视觉元素是定义通过子类时,可以动态地改变,所以你可以实现网格,栈,圆形布局,动态改变布局,或你能想象的任何类型的安排。
创建步骤:
(1)子类化UICollectionViewFlowLayout
(2)创建UIColletionView
(3)实现代理:UICollectionViewDataSource、UICollectionViewDelegate及第一步子类化后的布局代理
下面通过通过一个简单的demo来说明:
ViewController.m
#import "ViewController.h"#import "ClassMatesCell.h"#import "HPCollectionViewWaterfallLayout.h"@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,HPCollectionViewDelegateWaterfallLayout>@property (nonatomic,strong) UICollectionView *colletionView;@property (nonatomic,strong) NSMutableArray *listData;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSDictionary *dic1 = @{@"stu_name" : @"文字1"}; NSDictionary *dic2 = @{@"stu_name" : @"文字2"}; NSDictionary *dic3 = @{@"stu_name" : @"文字3"}; NSDictionary *dic4 = @{@"stu_name" : @"文字4"}; NSDictionary *dic5 = @{@"stu_name" : @"文字5"}; NSDictionary *dic6 = @{@"stu_name" : @"文字6"}; NSDictionary *dic7 = @{@"stu_name" : @"李晓7"}; NSDictionary *dic8 = @{@"stu_name" : @"文字8"}; NSDictionary *dic9 = @{@"stu_name" : @"文字9"}; NSDictionary *dic10 = @{@"stu_name" : @"文字10"}; _listData =[NSMutableArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5,dic6,dic7,dic8,dic9,dic10 ,nil]; [self createCollectionView];}- (void)createCollectionView{ if (!_colletionView) { HPCollectionViewWaterfallLayout *layout = [[HPCollectionViewWaterfallLayout alloc] init]; layout.delegate=self; _colletionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; _colletionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; _colletionView.dataSource = self; _colletionView.delegate = self; _colletionView.backgroundColor = [UIColor clearColor]; [_colletionView registerClass:[ClassMatesCell class] forCellWithReuseIdentifier:@"colletionviewcell"]; [self.view addSubview:_colletionView]; } else { [_colletionView reloadData]; }}#pragma mark - UIColltionView datasource- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.listData.count;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ ClassMatesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"colletionviewcell" forIndexPath:indexPath]; NSUInteger row = [indexPath row]; cell.tag = row + 1000; if ([self.listData count]>0) { NSDictionary *rowData = [self.listData objectAtIndex:(row)]; [cell.imageView setImage:[UIImage imageNamed:@"money"]]; //内容 cell.nameLabel.text = [rowData objectForKey:@"stu_name"]; } return cell;}#pragma mark - HPCollectionViewDelegateWaterfallLayout-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout interItemSpaceForSection:(NSUInteger)section{ return 0;}-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ return CGSizeMake(100, 110);}-(NSUInteger)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout numberOfColumnsForSection:(NSUInteger)section{ return section+3;}-(CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout preferredColumnSpaceForSection:(NSUInteger)section{ return 3;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
想进一步了解UIColletionView使用方法,请访问:https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40012334-CH1-SW1
0 0
- CollectionView的使用
- collectionView的使用iOS
- iOS collectionView的使用
- CollectionView Header的使用
- CollectionView 使用
- collectionview使用
- IOS学习之collectionView的使用
- 使用CSStickyHeaderFlowLayout实现头部固定的CollectionView
- CollectionView和layout的使用,瀑布流
- iOS 使用collectionView的无限轮播图
- iOS collectionView使用
- collectionview flowLayout使用
- iOS CollectionView简单使用
- collectionView使用总结
- collectionView纯代码使用
- CollectionView使用入门
- storyboard使用collectionview
- 关于iOS10使用collectionview
- 基于UML和ASP.NET实现三层B/S结构系统开发
- mysql yum 安装
- maven pom.xml基本用法
- Ubuntu 12.04 SVN 服务器搭建
- Activiti初学者教程
- CollectionView的使用
- Beyond Compare3 添加到右键菜单
- XML中实体的概念
- ubuntu12.04安装中文输入法
- ios app 实现热更新(无需发新版本实现app添加新功能)
- 修改PHP上传文件大小限制的方法
- 升级Xcode 导致插件失效的解决办法
- IntelliJ Idea 常用快捷键
- 我的Android进阶之旅------>Android利用Sensor(传感器)实现水平仪功能的小例