swift - 之 UICollectionView的用法/自定义流布局
具体代码如下:
1、声明
var hCollectionView:UICollectionView?
var layout:UICollectionViewFlowLayout? let courser = [
["name":"jack chen","pic":"1.jpeg"],
["name":"jack chen","pic":"1.jpeg"],
["name":"jack chen","pic":"1.jpeg"],
["name":"jack chen","pic":"1.jpeg"],
["name":"jack chen","pic":"1.jpeg"]
]
2、创建流布局,并初始化cellevtionView
// 设置布局流格式
layout = UICollectionViewFlowLayout()
layout?.itemSize = CGSize(width:self.view.frame.width/-,height:)
// 列间距,行间距,偏移
layout?.minimumInteritemSpacing = //列间距
layout?.minimumLineSpacing = //行间距
// layout?.sectionInset = UIEdgeInsets(top:5,left:0,bottom:0,right:0) self.hCollectionView = UICollectionView.init(frame: CGRect(x:,y:,width:kScreenWidth,height:kScreenHeight), collectionViewLayout: layout!)
self.hCollectionView?.delegate = self
self.hCollectionView?.dataSource = self
self.hCollectionView?.register(UINib.init(nibName: "hCell", bundle: nil), forCellWithReuseIdentifier: "hCell")
self.hCollectionView?.backgroundColor = UIColor.orange
//collectionview的偏移量
// self.hCollectionView?.contentInset = UIEdgeInsets(top:5,left:0,bottom:5,right:0) self.view.addSubview(self.hCollectionView!)
3、代理方法的实现
// 行数
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return courser.count
} // 获取单元格
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:hCell = self.hCollectionView?.dequeueReusableCell(withReuseIdentifier: "hCell", for: indexPath) as! hCell
//这里判断了下奇偶数
if indexPath.item% == {
cell.backgroundColor = UIColor.red
cell.bgImg.image = UIImage(named:self.courser[indexPath.item]["pic"]!)
cell.descLab.text = self.courser[indexPath.item]["name"] }else{
cell.backgroundColor = UIColor.yellow
cell.bgImg.image = UIImage(named:self.courser[indexPath.item]["pic"]!)
cell.descLab.text = self.courser[indexPath.item]["name"]
}
最终效果图:

上面这种的话,是标准的流布局,设定好item的尺寸,会随着屏幕向下平铺,但是还有一种自定义的流布局,例如一些门户类app的首页,左边有一个大的Item,右边是2个小的item,两个小的总高度和一个大的Item的高度是一致的!其实就是几个方法,东西不难,敲几遍都差不多了,这里仅做记录!
实现自定义流布局的话,主要是实现这3个方法:
// 内容区域总大小,不是可见区域
override var collectionViewContentSize: CGSize
// 所有单元格位置属性
override func layoutAttributesForElements(in rect: CGRect)
-> [UICollectionViewLayoutAttributes]?
// 这个方法返回每个单元格的位置和大小
override func layoutAttributesForItem(at indexPath: IndexPath)
-> UICollectionViewLayoutAttributes?
swift - 之 UICollectionView的用法/自定义流布局的更多相关文章
- Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面
网格UICollectionView除了使用流布局,还可以使用自定义布局.实现自定义布局需要继承UICollectionViewLayout,同时还要重载下面的三个方法: 1 2 3 4 5 6 7 ...
- UICollectionView 集合视图用法,自定义Cell
在View里面 //1.创建UICollectionViewFlowLayout UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFl ...
- Swift枚举的全用法
鉴于昨天开会部门会议讨论的时候,发现有些朋友对枚举的用法还是存在一些疑问,所以就写下这个文章,介绍下Swift下的枚举的用法. 基本的枚举类型 来,二话不说,我们先贴一个最基本的枚举: enum Mo ...
- SSAS维度设计中CustomRollupColumn的用法-自定义聚合方式
CustomRollupColumn说明:指定包含多维表达式的列,该表达式可用于聚合特性的度量值.这个属性覆盖给定度量值的AggregateFunction的属性. 解释:通常我们的度量值 ...
- android 自定义流布局。实现热门标签。开源库SimpleFlowLayout
前言 实际项目中需要实现一个 热门搜索 的栏目,类似下图: 由于 子项(子view) 中的文字是可变的,一行能显示的 子项 的个数也无法确定.需要支持自动换行和计算位置. 开源类库 我自己写了个 自定 ...
- swift 创建UICollectionView
// // CollectionViewController.swift // tab // // Created by su on 15/12/8. // Copyright © 2015年 ...
- Subclass UICollectionViewFlowLayout,自定义流布局
需求:为实现第一行显示一个,第二行以后显示两个 方案1:用系统自带的流布局,实现的效果是,若第二行只有一个,则系统默认会居中显示,不是左对齐(如下图),不符合项目要求. 方案2:自定义系统的UICol ...
- UICollectionView基础用法
初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; self.myColl ...
- Swift - 给UICollectionview设置组背景和圆角
钟情圆角怎么办 最近由于我们的UI钟情于圆角搞得我很方,各种圆角渐变,于是就有了下面这篇给UICollection组设置圆角和背景色的诞生,不知道在我们平时有没有遇到这样子的一些需求,就是按照每一组给 ...
随机推荐
- SpringKafka消费端配置类ConsumerConfig.java源码
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. ...
- jQuery请求维基百科[历史上的今天]
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 浅谈C# application.DoEvent作用
Application.DoEvents()的作用:处理所有的当前在消息队列中的Windows消息. private void button1_Click(object sender, EventAr ...
- python 将txt内容写入excel
#!/usr/bin/env python # -*- coding: utf-8 -* import os import xlrd import xlwt import datetime impor ...
- 解决Ubuntu16的风扇高速旋转问题(双显卡)
问题描述 自从我的双显卡的笔记本装上Ubuntu 14 后,风扇狂转.发热巨大.网上一搜索估计是显卡驱动不太行.最近英伟达的Nvidia Prime可以完美地切换双显卡,安装这个软件后,风扇就不会狂转 ...
- [转]SQL Server 「逾时过期」的处理方式
基本上 SQL Server 只要在处理大量数据的指令,如 INSERT INTO A SELECT * FROM B 在数据量很大的时候,很容易发生 Timeout ,也就是常见的「逾期过时」错误. ...
- 基于jquery带时间轴的图片轮播切换代码
基于jquery图片标题随小圆点放大切换.这是是一款带时间轴的图片轮播切换代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div id="decoroll2 ...
- SQL server 2005如何设置一个或几个字段唯一约束?
--建立是在三个字段上的唯一约束 alter table Tab add constraint uq_id unique(shipType, shipsession, Territory)
- C语言 · 龟兔赛跑预测
基础练习 龟兔赛跑预测 时间限制:1.0s 内存限制:512.0MB 锦囊1 模拟. 问题描述 话说这个世界上有各种各样的兔子和乌龟,但是研究发现,所有的兔子和乌龟都有一个共 ...
- Windows 2008 R2组策略之二——GPO初探
与前一篇博文发表已经相去月余了,实在是有些不好意思了.感谢51CTO的大编们将小文加了推荐,这使我更加觉得自己产出太少,难于回馈大家的厚爱. 今天将这个系列的第二篇奉献给大家,以伺视听. 在上篇小文中 ...