swift CollectionView学习

效果图:

源码:

ContModel.swift

import UIKit

class ContModel: NSObject {

    var title:String?
var image:String? func setValue(value: AnyObject?, forUndefinedKey key: String) { } }

ContViewCell.swift

cell截图

import UIKit

class ContViewCell: UICollectionViewCell {

    @IBOutlet weak var img: UIImageView!
@IBOutlet weak var titleLb: UILabel! var model:ContModel?{
//重写set方法
didSet{
img.image = UIImage(named: model!.image! )
titleLb.text = model?.title
}
} override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.layer.cornerRadius = 10
self.clipsToBounds = true
} }

ViewController.swift

import UIKit

class ViewController: UIViewController {

    var collectionView:UICollectionView?
var dataArr:NSMutableArray?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
prepareData()
setupCollectionView()
} func prepareData() {
dataArr = NSMutableArray()
for i in 0..<7
{
let model = ContModel()
model.image = "image\(i)"
model.title = "xxx\(i)"
dataArr?.add(model)
}
} func setupCollectionView() {
let layout = UICollectionViewFlowLayout();
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: 300, height: 400)
// layout.minimumLineSpacing = 5.0
layout.minimumInteritemSpacing = 10.0
let col = UICollectionView(frame:CGRect(x: 0, y: 100, width: 375, height: 400), collectionViewLayout: layout)
col.dataSource = self
col.backgroundColor = .clear;
col.register(UINib.init(nibName: "ContViewCell", bundle: nil), forCellWithReuseIdentifier: "ContViewCell")
collectionView = col
view.addSubview(col)
} } extension ViewController:UICollectionViewDataSource
{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
} func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return (dataArr?.count)!
} func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContViewCell", for: indexPath) as! ContViewCell
let model = dataArr?[indexPath.row]
cell.model = model as! ContModel?
return cell
} func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 200, height: 200)
} }

collectionView的方法和objc中的使用方法类似

这几有几个重点需要注意

1.使用UIVisualEffectView(iOS8以后才有的View)给view添加Effective效果

2.怎么自定义uicollectionViewCell

3.怎么创建自定义model,并重写set方法

swift学习 - collectionView的更多相关文章

  1. 【swift学习笔记】二.页面转跳数据回传

    上一篇我们介绍了页面转跳:[swift学习笔记]一.页面转跳的条件判断和传值 这一篇说一下如何把数据回传回父页面,如下图所示,这个例子很简单,只是把传过去的数据加上了"回传"两个字 ...

  2. 今天开始Swift学习

    今天开始Swift学习  在此记录笔记  以备之后查阅! allenhuang

  3. iOS ---Swift学习与复习

    swift中文网 http://www.swiftv.cn http://swifter.tips/ http://objccn.io/ http://www.swiftmi.com/code4swi ...

  4. 12套swift学习资源分享

    虽然objective-c编程语言在过去很长一段时间都是iOS应用开发的基础语言,且很多iOS开发者对其也深爱有佳,但是随着swift编程语言的问世,迅速发展为开发者追捧的语言.且今年伴随着swift ...

  5. Swift学习之常用UI的使用

    Swift学习之常用UI的使用 最近笔者在开始学习苹果最新的编程语言,因为笔者认为,苹果既然出了这门语言就绝对不会放弃,除非苹果倒闭了(当然这里知识一个玩笑). 所以在不久的将来,swift绝对是iO ...

  6. [转]swift 学习资源 大集合

    今天看到了一个swift的学习网站,里面收集了很多学习资源 [转自http://blog.csdn.net/sqc3375177/article/details/29206779] Swift 介绍 ...

  7. swift 学习资源 大集合

    今天看到一个swift学习网站,其中我们收集了大量的学习资源 Swift 介绍 Swift 介绍 来自 Apple 官方 Swift 简单介绍 (@peng_gong) 一篇不错的中文简单介绍 [译] ...

  8. Swift学习笔记(一)搭配环境以及代码运行成功

    原文:Swift学习笔记(一)搭配环境以及代码运行成功 1.Swift是啥? 百度去!度娘告诉你它是苹果最新推出的编程语言,比c,c++,objc要高效简单.能够开发ios,mac相关的app哦!是苹 ...

  9. swift学习:第一个swift程序

    原文:swift学习:第一个swift程序 最近swift有点火,赶紧跟上学习.于是,个人第一个swift程序诞生了... 新建项目

随机推荐

  1. hive 动态分区数设置

    当对hive分区未做设置时,报错如下: Caused by: org.apache.hadoop.hive.ql.metadata.HiveFatalException: [Error 20004]: ...

  2. 细细探究MySQL Group Replicaiton — 配置维护故障处理全集

             本文主要描述 MySQL Group Replication的简易原理.搭建过程以及故障维护管理内容.由于是新技术,未在生产环境使用过,本文均是虚拟机测试,可能存在考虑不周跟思路有误 ...

  3. SpringCloud网关ZUUL集成consul

    最近一直在搞基于springcloud的微服务开发,为了不限定微服务开发语言,服务发现决定采用consul不多说上代码 pom文件 <project xmlns="http://mav ...

  4. jQuery的发展史

    jQuery的发展史,你知道吗? 每天多学一点知识,就少写一行代码2006年1月,jQuery的第一个版本面世,至今已经有6年多了(注:这个时间点是截止至出书时间).虽然过了这么久,但它依然以其简洁. ...

  5. Webdriver API之操作(一)

    一. 控制浏览器 1. 控制浏览器大小 driver.set_window_size(480,800) #浏览器宽480,高800显示 dirver.maximize_window() #浏览器最大化 ...

  6. MongoDB数据库安装及配置环境终极教程(windows10系统)

    本文是笔者花时间踩坑踩生气了写出来的!转载请注明出处@http://www.cnblogs.com/tim100/!请尊重我的劳动成果!谢谢! 今天,给大家说说在windows10系统下MongoDB ...

  7. Xcode新建python项目

    1.找到电脑上安装Python的路径.OSX系统默认安装了python,默认的路径为/usr/bin/python.不确定的情况下,也可以打开命令行,用 whereis python 命令查看 2.打 ...

  8. Python实现二叉树的四种遍历

    对于一个没学过数据结构这门课程的编程菜鸟来说,自己能理解数据结构中的相关概念,但是自己动手通过Python,C++来实现它们却总感觉有些吃力.递归,指针,类这些知识点感觉自己应用的不够灵活,这是自己以 ...

  9. [转]Installing Memcached on Windows

    Installing Memcached on Windows 原文链接https://commaster.net/content/installing-memcached-windows   Sub ...

  10. Asp.Net 网站一键部署技术(下)

    上一篇我们讲了服务端的配置,现在我们来说说客户端的配置. 0x01: 使用Visual Studio发布向导创建发布配置文件 然后新建配置文件,因为我们的网站可能会发布到多个地方,比如发布一份内网测试 ...