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. 脚本语言:Xmas(一)

    很偶然的一个想法,在从北京回成都的高铁上:我想要一个计算器.于是在火车上花了十来个小时,完成了一个模型:能够处理+-*/的优先级,以及"()",比如:1+(3+2)*4.这已是一年 ...

  2. linux awk 命令详解

    awk是一个非常棒的数字处理工具.相比于sed常常作用于一整行的处理,awk则比较倾向于将一行分为数个"字段"来处理.运行效率高,而且代码简单,对格式化的文本处理能力超强.先来一个 ...

  3. android设备使用usb串口传输数据

    首先介绍两个开源项目一个是Google的开源项目:https://code.google.com/archive/p/android-serialport-api/ 另一个是我们这次介绍的开源项目:h ...

  4. JavaWeb开发之HttpServletResponse

    1. HttpServletResponse简介 Web服务器回送给Web客户端的HTTP响应消息分为三个部分:状态行,响应消息头,响应体. Servlet API中定义了ServletRespons ...

  5. java多线程基本概述(七)——join()方法

    在很多情况下,主线程创建并启动子线程,如果子线程中有大量的耗时运算,主线程将早于子线程结束,如果想让主线程等待子线程结束后再结束,那么我们可以使用join()方法.调用join()方法的意思是当前线程 ...

  6. C++ count_if/erase/remove_if 用法详解

    每次使用这几个算法时都要去查CPP reference,为了能够加深印象,整理一下基本应用. cout/cout_if:  return the number of elements satisfyi ...

  7. 使用JS控制伪元素的几种方法

    一. 缘由: 本文源于在OSC社区中,有人提问如何用jq获取伪元素.我第一想法是强大的CSS Query应该可以获取伪元素吧. 然而事实上,CSS Query并不能.即我们不能通过$(":b ...

  8. 初学strurs基础

    Struts2基础学习总结 Struts 2是在WebWork2基础发展而来的. 注意:struts 2和struts 1在代码风格上几乎不一样. Struts 2 相比Struts 1的优点: 1. ...

  9. 设置spring-boot的logging

    spring-boot默认使用logback来记录logger,spring-boot的包里面org.springframework.boot.logging.logback路径下面有一些配置文件,默 ...

  10. python自动化测试应用-第6篇(WEB测试)--Selenium元素篇

    篇6                            python自动化测试应用-Selenium基础篇 --lamecho 1.1概要 大家好!我是lamecho(辣么丑),上一篇我们搭建好p ...