当前位置: 首页 > news >正文

网页设计公司网站设计广州安全教育平台账号登录入口

网页设计公司网站设计,广州安全教育平台账号登录入口,上海做外贸建站的专业公司,免费 搭建公司网站一,UICollectionView的简介 UICollectionView是iOS6之后引入的一个新的UI控件,它和UITableView有着诸多的相似之处,其中许多代理方法都十分类似。简单来说,UICollectionView是比UITbleView更加强大的一个UI控件,有如下…

一,UICollectionView的简介
UICollectionView是iOS6之后引入的一个新的UI控件,它和UITableView有着诸多的相似之处,其中许多代理方法都十分类似。简单来说,UICollectionView是比UITbleView更加强大的一个UI控件,有如下几个方面:

1.支持水平和垂直两种方向的布局
2.通过layout配置方式进行布局
3.类似于TableView的cell特性外,collectionView中的item大小和位置可以自由定义;
4.通过layout布局回调的代理方法,可以动态的定制每个item的大小和collection的大体布局属性
5.更加强大一点,完全自定义一套layout布局方案,可以实现意想不到的效果。

设置静态的布局

实现一个最简单的九宫格类布局

- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.//创建一个layout布局类UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc]  init];flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//flowLayout.itemSize = CGSizeMake((self.view.frame.size.width - 80) / 3, (self.view.frame.size.width - 80) / 3);//flowLayout.itemSize = CGSizeMake(100, 100 );//flowLayout.minimumLineSpacing = 20;//flowLayout.minimumInteritemSpacing = 20;flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);//创建collectionView,通过一个布局策略layout来创建self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];//注册cell[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];//设置代理self.collectionView.delegate = self;//设置数据源self.collectionView.dataSource = self;[self.view addSubview:self.collectionView];
}

这里有一点需要注意,collectionView在完成代理回调前,必须注册一个cell,类似如下:

//注册cell[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

这和tableView有些类似,又有些不同,因为tableView除了注册cell的方法外,还可以通过非注册的方法,当复用池中没有可用的cell时,可以返回nil,然后重新创建。

//tableView在从复用池中取cell的时候,有如下两种方法
//使用这种方式如果复用池中无,是可以返回nil的,我们在临时创建即可
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier;
//6.0后使用如下的方法直接从注册的cell类获取创建,如果没有注册 会崩溃
- (__kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);

因为UICollectionView是iOS6.0之前的新类,因此这里统一了从复用池中获取cell的方法,没有再提供可以返回nil的方式,并且在UICollectionView的回调代理中,只能使用从复用池中获取cell的方式进行cell的返回,其他方式会崩溃。

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];cell.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1];return cell;
}

然后实现其他的代理方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return 9;
}

在这里插入图片描述
上面我们写了一个简单九宫格的代码,所有的item都是一样大小的,但是有时候这样满足不了我们的需求,我们有时候可能也需要item不同大小。
在上面代码的基础上,删除控制item的大小的代码,再加入下面几行代码即可实现:

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{if (indexPath.row % 2 == 0) {return CGSizeMake(50, 50);} else {return CGSizeMake(80, 80);}
}

在这里插入图片描述

设置动态布局

自定义FlowLayout进行瀑布流布局
首先,我们新建一个文件继承于UICollectionViewFlowLayout:

#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface MyLayout : UICollectionViewFlowLayout
@property (nonatomic, assign)int itemCount;
@endNS_ASSUME_NONNULL_END

前面说过,UICollectionViewFlowLayout是一个专门用来管理collectionView布局的类,因此,collectionView在进行UI布局前,会通过这个类的对象获取相关的布局信息,FlowLayout类将这些布局信息全部存放在了一个数组中,数组中是UICollectionViewLayoutAttributes类,这个类是对item布局的具体设置,以后咱们在讨论这个类。总之,**FlowLayout类将每个item的位置等布局信息放在一个数组中,在collectionView布局时,会调用FlowLayout类layoutAttributesForElementsInRect:方法来获取这个布局配置数组。因此,我们需要重写这个方法,返回我们自定义的配置数组,**另外,FlowLayout类在进行布局之前,会调用prepareLayout方法,所以我们可以重写这个方法,在里面对我们的自定义配置数据进行一些设置。

简单来说,自定义一个FlowLayout布局类就是两个步骤:

1、设计好我们的布局配置数据 prepareLayout方法中

2、返回我们的配置数组 layoutAttributesForElementsInRect方法中

#import "MyLayout.h"@implementation MyLayout {NSMutableArray* attributeArray;
}
- (void)prepareLayout {attributeArray = [[NSMutableArray alloc] init];[super prepareLayout];float WIDTH = ([UIScreen mainScreen].bounds.size.width - self.sectionInset.left - self.sectionInset.right - self.minimumInteritemSpacing) / 2;CGFloat colHeight[2] = {self.sectionInset.top, self.sectionInset.bottom};for(int i = 0; i < self.itemCount; i++) {NSIndexPath* index = [NSIndexPath indexPathForItem:i inSection:0];UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:index];CGFloat height = arc4random() % 150 + 40;int width = 0;if (colHeight[0] < colHeight[1]) {colHeight[0] = colHeight[0] + height + self.minimumLineSpacing;width = 0;} else {colHeight[1] = colHeight[1] + height + self.minimumLineSpacing;width = 1;}attributes.frame = CGRectMake(self.sectionInset.left + (self.minimumInteritemSpacing + WIDTH) * width, colHeight[width] - height - self.minimumLineSpacing, WIDTH, height);[attributeArray addObject:attributes];}if (colHeight[0] > colHeight[1]) {self.itemSize = CGSizeMake(WIDTH, (colHeight[0] - self.sectionInset.top) * 2 / self.itemCount - self.minimumLineSpacing);} else {self.itemSize = CGSizeMake(WIDTH, (colHeight[1] - self.sectionInset.top) * 2 / self.itemCount - self.minimumLineSpacing);}
}
- (NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {return attributeArray;
}
@end

自定义完成FlowLayout后,在View Controller中使用

#import "ViewController.h"
#import "MyLayout.h"
#import "CollectionViewCell.h"
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];MyLayout* layout = [[MyLayout alloc] init];layout.scrollDirection = UICollectionViewScrollDirectionVertical;layout.itemCount = 100;UICollectionView* collectView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];collectView.delegate = self;collectView.dataSource = self;[collectView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];[self.view addSubview:collectView];// Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return 100;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];cell.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1];return cell;
}
@end

在这里插入图片描述

http://www.yayakq.cn/news/515632/

相关文章:

  • 百度推广方案怎么写政务网站优化
  • 网站建设 amp 金手指排名效果好旅游开发 网站建设
  • flash网站好做seo不上海市建设工程 安全质量网站
  • 防蜘蛛抓取网站代码160加工网
  • 怎么做企业网站图片 套网站模板下载 迅雷下载 迅雷下载地址
  • 网站建设 推广就选网沃科技怎样学做网站
  • 企业网站建设word深圳家装公司十大排名
  • 建设酒类产品网站的好处wordpress 后台底部修改
  • 方法数码做的网站怎么样国家职业建设中心网站
  • 免费制作二维码的网站湖北田源建设工程有限公司网站
  • ps手机网站制作代驾app软件开发公司
  • 论坛网站怎么做最新的新闻内容
  • 海外网站seo三亚网站设计公司
  • 梅县区住房和城乡规划建设局网站揭阳模板网站建站
  • 一诺千金 网站建设手机怎么做网站教程
  • 广西公司做网站国内最新新闻事件
  • 哪个云服务器便宜又好佛山seo优化电话
  • 南昌做网站优化网页前端模板网站
  • wordpress精美网站html怎么设置网站吗
  • 西安网站建设和推广公司做网站建设公司起名
  • 清河做网站广东省住房和城乡建设厅网站
  • 做设计一般在那个网站找图网站logo如何修改
  • 怎么做徐州市环保局网站备案表自然志 wordpress主题
  • 毕业设计做网站 服务器怎么弄用软件什么做网站
  • 青海网站建设系统湖南企业竞价优化首选
  • 漆包线 东莞网站建设网站建设哪家好就推 鹏博资讯
  • 朝阳网站建设是什么意思Wordpress如何加联盟广告
  • 网站建设学那些课网站开发会计科目
  • WordPress全站广告订阅号做微网站
  • 申请网站到哪里乐清网站优化