一、网格UICollectionView最典型的例子是iBooks。其主要属性如下:

1,layout
该属性表示布局方式,有Flow、Custom两种布局方式。默认是Flow流式布局。
2,Accessories
是否显示页眉和页脚
3,各种尺寸属性
Cell Size:单元格尺寸
Header Size:页眉尺寸
Footer Size:页脚尺寸
Min Spacing:单元格之间间距
Section Insets:格分区上下左右空白区域大小。
二、流布局的简单样例
1,先创建一个ASimple View Application,删除默认的View Controller,拖入一个Collection
View Controller到界面上,这时我们可以看到已经同时添加了Collection View和Collection View
Cell控件。
2,勾选Collection View Controller属性面板里的Is Initial View Controller复选框,设置为启动视图控制器。
3,在Collection View Cell里拖入一个Image View和Label并摆放好位置和大小,用于显示图标和名称。
4,设置Image View的tag为1,Label的tag为2,Colletion View Cell的Identifier为DesignViewCell。
效果图如下:
 

--- 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import UIKit
 
class ViewController: UICollectionViewController {
     
    //课程名称和图片,每一门课程用字典来表示
    let courses = [
        ["name":"Swift","pic":"swift.png"],
        ["name":"OC","pic":"oc.jpg"],
        ["name":"java","pic":"java.png"],
        ["name":"php","pic":"php.jpeg"]
    ]
     
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
         
        // 已经在界面上设计了Cell并定义了identity,不需要注册CollectionViewCell
        //self.collectionView.registerClass(UICollectionViewCell.self,
        //  forCellWithReuseIdentifier: "ViewCell")
        //默认背景是黑色和label一致
        self.collectionView?.backgroundColor = UIColor.whiteColor()
         
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
     
    // CollectionView行数
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int)
        -> Int {
        return courses.count;
    }
     
    // 获取单元格
    override func collectionView(collectionView: UICollectionView,
        cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // storyboard里设计的单元格
        let identify:String = "DesignViewCell"
        // 获取设计的单元格,不需要再动态添加界面元素
        let cell = self.collectionView?.dequeueReusableCellWithReuseIdentifier(
            identify, forIndexPath: indexPath) as UICollectionViewCell
        // 从界面查找到控件元素并设置属性
        (cell.contentView.viewWithTag(1) as! UIImageView).image =
            UIImage(named: courses[indexPath.item]["pic"]!)
        (cell.contentView.viewWithTag(2) as! UILabel).text = courses[indexPath.item]["name"]
        return cell
    }
}

--- Main.storyboard ---

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="fNw-sO-QDe">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
    </dependencies>
    <scenes>
        <!--View Controller-->
        <scene sceneID="wxT-ij-Le6">
            <objects>
                <collectionViewController id="fNw-sO-QDe" customClass="ViewController" customModule="SwiftInAction_008_015" customModuleProvider="target" sceneMemberID="viewController">
                    <collectionView key="view" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" id="pK1-nH-r5x">
                        <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
                        <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                        <color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
                        <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="UDB-fy-TG6">
                            <size key="itemSize" width="92" height="97"/>
                            <size key="headerReferenceSize" width="0.0" height="20"/>
                            <size key="footerReferenceSize" width="0.0" height="0.0"/>
                            <inset key="sectionInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
                        </collectionViewFlowLayout>
                        <cells>
                            <collectionViewCell opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" restorationIdentifier="ViewCell" reuseIdentifier="DesignViewCell" id="FKz-79-V4r">
                                <rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
                                <autoresizingMask key="autoresizingMask"/>
                                <view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
                                    <rect key="frame" x="0.0" y="0.0" width="92" height="97"/>
                                    <autoresizingMask key="autoresizingMask"/>
                                    <subviews>
                                        <imageView userInteractionEnabled="NO" tag="1" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ZmJ-ad-pZs">
                                            <rect key="frame" x="4" y="4" width="75" height="65"/>
                                        </imageView>
                                        <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" tag="2" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="U4J-Ys-QBF">
                                            <rect key="frame" x="4" y="71" width="75" height="21"/>
                                            <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                            <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
                                            <nil key="highlightedColor"/>
                                        </label>
                                    </subviews>
                                    <color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
                                </view>
                            </collectionViewCell>
                        </cells>
                        <collectionReusableView key="sectionHeaderView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" id="rmh-sg-HAy">
                            <rect key="frame" x="0.0" y="0.0" width="600" height="20"/>
                            <autoresizingMask key="autoresizingMask"/>
                        </collectionReusableView>
                        <connections>
                            <outlet property="dataSource" destination="fNw-sO-QDe" id="LWA-AL-0di"/>
                            <outlet property="delegate" destination="fNw-sO-QDe" id="RRE-KH-WU2"/>
                        </connections>
                    </collectionView>
                </collectionViewController>
                <placeholder placeholderIdentifier="IBFirstResponder" id="tTf-V2-wvH" userLabel="First Responder" sceneMemberID="firstResponder"/>
            </objects>
            <point key="canvasLocation" x="174" y="137"/>
        </scene>
    </scenes>
</document>

Swift - 使用网格(UICollectionView)进行流布局的更多相关文章

  1. Swift - 设置网格UICollectionView的单元格间距

    要设置单元格cell的间距(水平间距,垂直间距)可进行如下设置: 方法1:在storyboard中设置 选择Collection View后在面板里设置Min Spacing相关属性(这里也可以设置单 ...

  2. Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面

    网格UICollectionView除了使用流布局,还可以使用自定义布局.实现自定义布局需要继承UICollectionViewLayout,同时还要重载下面的三个方法: 1 2 3 4 5 6 7 ...

  3. Swift:用UICollectionView整一个瀑布流

    本文的例子和Swift版本是基于Xcode7.2的.以后也许不知道什么时候会更新. 我们要干点啥 用新浪微博的Open API做后端来实现我们要提到的功能.把新浪微博的内容,图片和文字展示在colle ...

  4. 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱

    前言 API 不熟悉导致的问题,想当然的去理解果然会出问题,这里记录一下 UICollectionView 使用问题. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cn ...

  5. 前不久一个swift项目用uicollectionview 用sdwebimage 加载图片,发生内存猛增,直接闪退的情况,简单说一下解决方案。

    1.首先在appdelegate方法 didFinishLaunchingWithOptions SDImageCache.sharedImageCache().maxCacheSize=1024*1 ...

  6. [Swift]LeetCode1001. 网格照明 | Grid Illumination

    On a N x N grid of cells, each cell (x, y) with 0 <= x < N and 0 <= y < N has a lamp. In ...

  7. Swift UITableView嵌套UICollectionView点击事件冲突(点击事件穿透)

    不管是啥都响应tableviewcell class JYShopCertificationCell: UITableViewCell { override func hitTest(_ point: ...

  8. ios项目里扒出来的json文件

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  9. Github上关于iOS的各种开源项目集合(强烈建议大家收藏,查看,总有一款你需要)

    下拉刷新 EGOTableViewPullRefresh - 最早的下拉刷新控件. SVPullToRefresh - 下拉刷新控件. MJRefresh - 仅需一行代码就可以为UITableVie ...

随机推荐

  1. 边框圆角化方式(原文链接http://www.cnblogs.com/SJP666/p/4678730.html)

    第一种方法:如果是CSS2.2的话,可以简单写一个制作圆角矩形,分上中下裁成三张图 <title>CSS3实现圆角</title> <style type="t ...

  2. Cloud Engine

    Cloud Engine:大杀器如何炼成   郑昀 创建于2016/6/18 最后更新于2016/6/19 点击查看我的<如何从零搭建一个技术平台>,这是一个系列.转载时请注明“转载自旁观 ...

  3. [教程]安装青鸟云Web服务器

    青鸟云Web服务器 支持的安装环境: Windows XP (32bit) Windows Server 2003/R2 (32bit) Windows 7 (32/64bit) Windows Se ...

  4. Oculus Rift DK2 安装所需电脑配置

    系统支持:眼下使用的SDK0.4支持windows7,windows8,windows8.1.Mac(10.8,10.9): 硬件配置:官方给的标准配置为2.0+GHz以上CPU.2GB内存.兼容Di ...

  5. 工作线程AfxBeginThread的使用

    工作线程通常用来执行一些后台任务,如:数据计算.后台杀毒等等.因为不需要创建窗口和处理用户消息,编写比较容易,在程序中只要调用AfxBeginThread 函数就可以创建并启动一个工作线程了. Afx ...

  6. CSS——float属性备忘笔记

    通过指定CSS属性float的值,从而使元素向左或向右浮动,然后由后继元素向上移动以填补前面元素的浮动而空出的可用空间.CSS的float属性,作用就是改变块元素对象的默认显示方式,HTML标签设置了 ...

  7. 公钥password学中的素数以及对称加密

        password学.一向被人们觉得门槛非常高,特别高端...这也是实际,可是这决不意味着普通人无法了解它的精髓.对于喜欢画圆的人来讲,即便是理解了password技术背后的哪怕一点理论,也是激 ...

  8. 极限挑战—C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码)

    原文:极限挑战-C#100万条数据导入SQL SERVER数据库仅用4秒 (附源码) 实际工作中有时候需要把大量数据导入数据库,然后用于各种程序计算,本实验将使用5中方法完成这个过程,并详细记录各种方 ...

  9. 基于visual Studio2013解决C语言竞赛题之1051数的顺序

       题目 解决代码及点评 /* 功能:自然数N一般写成如下形式: N=d[k]d[k-1]d[1] (d[1]-d[k] 均是十进制数字) 如果d[i+1]>d[i] (i=k-1 ...

  10. “add measurements”(添加度量)菜单问题