swift系统学习控件篇:UITableView+UICollectionView
工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码:
UITableView:
//
// ViewController.swift
// UItableView-swift
//
// Created by shaoting on 16/3/23.
// Copyright © 2016年 9elephas. All rights reserved.
// import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
var ary:[String] = ["","",""];
override func viewDidLoad() {
super.viewDidLoad()
//定义一个UITableView
let mytableView = UITableView(frame: CGRectMake(, , UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height), style: UITableViewStyle.Plain)
self.view.addSubview(mytableView)
mytableView.delegate = self
mytableView.dataSource = self
mytableView.backgroundColor = UIColor.whiteColor()
mytableView.rowHeight =
// Do any additional setup after loading the view, typically from a nib.
}
//实现UITableViewDataSource必须实现的两个方法
//行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return
}
//返回cell
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "少停"
if indexPath.row % == {
cell.imageView?.image = UIImage(named: "")
}else{
cell.imageView?.image = UIImage(named: "")
}
return cell
}
//section个数
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return
}
//页眉
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == {
return ""
}else if section == {
return ""
}else{
return ""
}
}
//页脚
func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "..."
}
//右侧索引
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
return ary
}
//可以编辑
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
if indexPath.section == {
return false
}else{
return true
}
}
//可以移动
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true;
}
//选中
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == {
print("选中1区第\(indexPath.row)行")
}else if indexPath.section == {
print("选中2区第\(indexPath.row)行")
}else{
print("选中3区第\(indexPath.row)行")
}
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }
UICollectionView:
//
// ViewController.swift
// CollectionView
//
// Created by shaoting on 16/3/25.
// Copyright © 2016年 9elephas. All rights reserved.
// import UIKit class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
var collectionView : UICollectionView?
var dataAry = NSMutableArray()
override func viewDidLoad() {
super.viewDidLoad()
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSizeMake(, )
flowLayout.scrollDirection = UICollectionViewScrollDirection.Vertical // 设置垂直显示
flowLayout.sectionInset = UIEdgeInsetsMake(, , , ) //设置边距
flowLayout.minimumLineSpacing = //设置相邻layout的上下
flowLayout.minimumInteritemSpacing = //设置相邻layout的左右
flowLayout.headerReferenceSize = CGSizeMake(, ) collectionView = UICollectionView(frame: CGRect(x: , y: , width: self.view.frame.size.width, height: self.view.frame.size.height), collectionViewLayout: flowLayout)
collectionView?.delegate = self
collectionView?.dataSource = self
collectionView?.alwaysBounceVertical = true
self.view.addSubview(collectionView!)
collectionView?.backgroundColor = UIColor.whiteColor()
collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell") //注册一个cell // Do any additional setup after loading the view, typically from a nib.
} func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ return
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell
cell.backgroundColor = UIColor.redColor() return cell
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} }
源码下载地址:
UITableView:
https://github.com/pheromone/UItableView-swift
http://download.csdn.net/detail/shaoting19910730/9474696
UICollectionView
http://download.csdn.net/detail/shaoting19910730/9474700
https://github.com/pheromone/CollectionView
swift学习网站;
swift系统学习控件篇:UITableView+UICollectionView的更多相关文章
- swift系统学习控件篇:UIbutton+UIlabel+UITextField+UISwitch+UISlider
工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIButton+UILabel // // ViewController.swift // ...
- swift系统学习控件篇:UIProgressView+NSTimer+UIstepper+UIAlertController
工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIProgressView+NSTimer+UIstepper UIStepper UIP ...
- 一步一步学android之控件篇——ScrollView
一个手机的屏幕大小是有限的,那么我要显示的东西显示不下怎么办?这就会使用到ScrollView来进行滚动显示,他的定义如下: 可以看到ScrollView是继承于FrameLayout的,所以Scro ...
- 用swift开发仪表盘控件(一)
苹果swift刚刚推出不久,接触到这个语言是一个偶然的机会,无聊之余随便看了下它的语法: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveW5tYW95b2 ...
- DevExpress学习系列(控件篇):GridControl的基本应用
一般属性设置 不显示分组框:Gridview->Option View->Show Group Panel=false 单元格不可编辑:gridcontrol -->gridview ...
- WPF学习笔记 控件篇 属性整理【1】FrameworkElement
最近在做WPF方面的内容,由于好多属性不太了解,经常想当然的设置,经常出现自己未意料的问题,所以感觉得梳理下. ps:先补下常用控件的类结构,免得乱了 .NET Framework 4.5 Using ...
- openlayers4 入门开发系列之地图导航控件篇(附源码下载)
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...
- [Swift]键盘遮挡控件
键盘遮挡控件: super.viewDidLoad(){ // Do any additional setup after loading the view, typically from a nib ...
- 用swift开发仪表盘控件(二)
二.代码分析 这个控件本质就是从UIView继承的一个类而已.所以整个代码事实上就是一个定制的UIView类. 依据UIView的规则进行例如以下初始化: required init(coder aD ...
随机推荐
- MyBatis核心配置文件模版
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLI ...
- Extjs中处理mouseover的闪烁问题
在使用mouseover和mouseout实现鼠标滑动效果并且target的dom较复杂时,可能会产生闪烁现象,产生这个问题的一个原因是mouseover事件本身的冒泡特性和target dom的子元 ...
- 超详细的Xcode代码格式化教程,可自定义样式。
超详细的Xcode代码格式化教程,可自定义样式. 为什么要格式化代码 当团队内有多人开发的时候,每个人写的代码格式都有自己的喜好,也可能会忙着写代码而忽略了格式的问题.在之前,我们可能会写完代码后,再 ...
- 如何在win7下配置IIS?
- 开源项目导入eclipse的一般步骤[转]
下载到开源项目后,我们还是希望导入到eclipse中还看,这样要方便点,一般的步骤是这样的 打开源代码目录, 如果看到里面有.calsspath .project文件,那么说明这个项目本来就是ec ...
- Eclipse 汉化包
http://www.eclipse.org/babel/downloads.php 下载地址,具体操作请百度. http://subclipse.tigris.org/update_1.6.x SV ...
- java之trycatchfinally代码块与return,throw的执行顺序的探索
时光荏苒,转眼间毕业都半年了,java编程也五个月了.写代码的过程中,会经常遇到解决代码抛异常的情况.平时只注重完成功能,也没太注意try_catch_finally的内在执行顺序,只知道表面的现象: ...
- [Jquery]网页定位导航特效
描述:左右联动的导航,非常适合展示页面内容多,区块划分又很明显的,点击右边固定导航项时,左边的内容跟着切换.滑动滚动条的时候,右边的导航也随着左边的展示而进行高亮切换. 思路:比较滚动距离和楼层距离( ...
- html圆角提示效果
<fieldset> <legend>标题</legend> 内容 </fieldset>
- EntLib Unity父类的依赖注入问题
Unity的注入有3种方式:构造函数.[Dependency]属性.[InjectionMethod]方法.这3种方式涉及到的interface或class都会去Registrations里找,找不到 ...