工作之余,学习下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学习网站;

http://www.helloswift.com.cn/

swift系统学习控件篇:UITableView+UICollectionView的更多相关文章

  1. swift系统学习控件篇:UIbutton+UIlabel+UITextField+UISwitch+UISlider

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIButton+UILabel // // ViewController.swift // ...

  2. swift系统学习控件篇:UIProgressView+NSTimer+UIstepper+UIAlertController

    工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UIProgressView+NSTimer+UIstepper UIStepper UIP ...

  3. 一步一步学android之控件篇——ScrollView

    一个手机的屏幕大小是有限的,那么我要显示的东西显示不下怎么办?这就会使用到ScrollView来进行滚动显示,他的定义如下: 可以看到ScrollView是继承于FrameLayout的,所以Scro ...

  4. 用swift开发仪表盘控件(一)

    苹果swift刚刚推出不久,接触到这个语言是一个偶然的机会,无聊之余随便看了下它的语法: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveW5tYW95b2 ...

  5. DevExpress学习系列(控件篇):GridControl的基本应用

    一般属性设置 不显示分组框:Gridview->Option View->Show Group Panel=false 单元格不可编辑:gridcontrol -->gridview ...

  6. WPF学习笔记 控件篇 属性整理【1】FrameworkElement

    最近在做WPF方面的内容,由于好多属性不太了解,经常想当然的设置,经常出现自己未意料的问题,所以感觉得梳理下. ps:先补下常用控件的类结构,免得乱了 .NET Framework 4.5 Using ...

  7. openlayers4 入门开发系列之地图导航控件篇(附源码下载)

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...

  8. [Swift]键盘遮挡控件

    键盘遮挡控件: super.viewDidLoad(){ // Do any additional setup after loading the view, typically from a nib ...

  9. 用swift开发仪表盘控件(二)

    二.代码分析 这个控件本质就是从UIView继承的一个类而已.所以整个代码事实上就是一个定制的UIView类. 依据UIView的规则进行例如以下初始化: required init(coder aD ...

随机推荐

  1. hdu-----(1532)Drainage Ditches(最大流问题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. C# string 数组 每个元素 加上单引号,每一个都被包含在单引号内

    在拼接SQL的时候经常会遇到此类问题,尤其是in查询的时候,内容是一段 单引号的 字符的时候 strWhere += " a.EC101_WRYBH  IN (" + string ...

  3. Java 集合系列 07 List总结(LinkedList, ArrayList等使用场景和性能分析)

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  4. Checked 和 UnChecked 异常 的使用场合

    异常的概念  任何的异常都是Throwable类(为何不是接口??),并且在它之下包含两个子类Error / Exception,而Error仅在当在Java虚拟机中发生动态连接失败或其它的定位失败的 ...

  5. SQL的常用语句

    select * from g_members where id between '16' and '31' order by id desc 倒序排列 select * from g_members ...

  6. GridView72变 控件

    快速预览:GridView 无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单 DropDownList结合GridView和CheckBox结 ...

  7. CentOS命令登录MySQL时,报错ERROR 1045 (28000):

    CentOS命令登录MySQL时,报错ERROR 1045 (28000): Access denied for user root@localhost (using password: NO)错误解 ...

  8. java 面向对象编程 --第十二章 JDK常用类

    1.  系统类 java.lang包   System类 sys.out;sys.exit;sys.gc; sys.currentTimeMillis();----得到从1970-01-01到当前时间 ...

  9. Apache—DBUtils框架简介

    转载自:http://blog.csdn.net/fengdongkun/article/details/8236216 Apache—DBUtils框架简介.DbUtils类.QueryRunner ...

  10. 知名杀毒软件Mcafee(麦咖啡)个人版 资源汇总兼科普(来自卡饭)

    虽然早已不是用咖啡了,但我也实时关注的咖啡的一举一动,潜水看帖日久,发现小白众多,好多有价值的帖子淹没于帖海当中,甚是惋惜.     我有如下建议      1.咖啡区管理层,能否吧一些优秀的资源教程 ...