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 ...
随机推荐
- C/C++源代码到可执行程序的过程详解
编译,编译程序读取源程序(字符流),对之进行词法和语法的分析,将高级语言指令转换为功能等效的汇编代码,再由汇编程序转换为机器语言,并且按照操作系统对可执行文件格式的要求链接生成可执行程序. 源代码-- ...
- JS基础--问题记录
1. {}var a={};{}是一个空的对象,是 new Object();的简写. 2.判断元素是存在 //jQuery 对象中元素的个数.当前匹配的元素个数. size 将返回相同的值. if ...
- Xcode8 及iOS10适配问题汇总
上点干货,目前得知的一些bug解决汇总:iOS10相册相机闪退bughttp://www.jianshu.com/p/5085430b029fiOS 10 因苹果健康导致闪退 crashhttp:// ...
- postgres 正则表达式
PostgreSQL正则表达式 基础: Operator Description Example ~ Matches regular expression, case sensitive 'thoma ...
- 转载:Clear Float
众所周知,平时在写HTML代码时,难免少不了使用Float样式,这样一来,假使您没有清除浮动,那么有浮动元素的父元素容器将元素将无法自动撑 开.换句简单好理解的话来说,假如你在写CODE时,其中div ...
- c#中的常用ToString()方法总结
c#中的常用ToString()方法总结 对于int,double等的tostring: C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToStri ...
- vs2016 vsto excel addin deploy error: vsto 无法解析属性type的值
https://social.msdn.microsoft.com/Forums/vstudio/en-US/ea33e391-21d7-4f54-92cb-c7af72f19c61/outlook- ...
- Jquery 表格操作,记录分页情况下,每一页中被用户勾选的信息
如下图,一个分页列表,用户可以随意勾选一条或多条信息,然后进行某种操作,如“提交”.但是有个问题:如果勾选了一条信息之后,点[下一页],那么上一页 勾选的条目被刷新掉了. 问题:如果用户需要在第1页, ...
- 一个Java递归程序
先来没事搜了一些面试题来做,其中一道:输入一个整数,求这个整数中每位数字相加的和? 思考:1.如何或得每一位数:假如是1234, 1234%10=4,得到个位:(1234/10)%10=3得到十位 ...
- 二模 (13)day1
第一题: 题目大意: N个发射站排成一排,求每个发射站左右第一个比它高的发射站. N<=1000000 解题过程: 1.前几天做poj的时候刚好在discuss里看到有一个神奇的东东叫单调栈,正 ...