Swift - 使用网格(UICollectionView)进行流布局

前端之家收集整理的这篇文章主要介绍了Swift - 使用网格(UICollectionView)进行流布局前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一、网格UICollectionView最典型的例子是iBooks。其主要属性如下:
1,layout
属性表示布局方式,有Flow、Custom两种布局方式。默认是Flow流式布局。

2,Accessories
是否显示页眉和页脚
3,各种尺寸属性
Cell Size:单元格尺寸
Header Size:页眉尺寸
Footer Size:页脚尺寸
Min Spacing:单元格之间间距
Section Insets:格分区上下左右空白区域大小。
二、流布局的简单样例
1,先创建一个应用Simple View Application,删除默认的 View Controller,拖入一个 Collection View Controller到界面上,这时我们可以看到已经同时添加Collection ViewCollection View Cell控件。
2,勾选 Collection View Controller属性面板里的 Is Initial View Controller复选框,设置为启动视图控制器。
3,在 Collection View Cell里拖入一个 Image ViewLabel并摆放好位置和大小,用于显示图标和名称
4,设置 Image Viewtag1Label2Colletion View CellIdentifierDesignViewCell
效果图如下:
--- ViewController.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
import UIKit
class ViewController: UICollectionViewController {
//课程名称图片,每一门课程用字典来表示
let courses = [
[ "name" : "Swift" , "pic" "swift.png" ],
"OC" "oc.jpg" "java" "java.png" "PHP" "PHP.jpeg" ]
]
override func viewDidLoad() {
super .viewDidLoad()
// 已经在界面上设计了Cell并定义了identity,不需要注册CollectionViewCell
//self.collectionView.registerClass(UICollectionViewCell.self,
// forCellWithReuseIdentifier: "DesignViewCell")
self.collectionView?.backgroundColor = UIColor.whiteColor()
}
@H_196_301@// CollectionView行数
func collectionView(collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return courses.count;
}
// 获取单元格
cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// storyboard里设计的单元格
let identify: String = "DesignViewCell"
// 获取设计的单元格,不需要再动态添加界面元素
let cell = (self.collectionView?.dequeueReusableCellWithReuseIdentifier(
identify,forIndexPath: indexPath))! as UICollectionViewCell
// 从界面查找到控件元素并设置属性
(cell.contentView.viewWithTag( 1 ) ! UIImageView).image =
UIImage(named: courses[indexPath.item][ ]!)
2 ! UILabel).text =
courses[indexPath.item][ ]
cell
}
func didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
@H_950_404@设置可重用标识
@H_950_404@设置cell的宽高和间距

原文链接:https://www.f2er.com/swift/324802.html

猜你在找的Swift相关文章