swift系统学习控件篇:UITableView+UICollectionView

时间:2023-03-09 00:19:41
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.****.net/detail/shaoting19910730/9474696

UICollectionView

http://download.****.net/detail/shaoting19910730/9474700

https://github.com/pheromone/CollectionView

swift学习网站;

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