工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码:
UITableView:
1 // 2 // ViewController.swift 3 // UItableView-swift 4 // 5 // Created by shaoting on 16/3/23. 6 // Copyright © 2016年 9elephas. All rights reserved. 7 // 8 9 import UIKit 10 11 class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{ 12 var ary:[String] = ["0","1","2"]; 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 //定义一个UITableView 16 let mytableView = UITableView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height), style: UITableViewStyle.Plain) 17 self.view.addSubview(mytableView) 18 mytableView.delegate = self 19 mytableView.dataSource = self 20 mytableView.backgroundColor = UIColor.whiteColor() 21 mytableView.rowHeight = 100 22 // Do any additional setup after loading the view, typically from a nib. 23 } 24 //实现UITableViewDataSource必须实现的两个方法 25 //行数 26 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 27 return 2 28 } 29 //返回cell 30 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 31 let cell = UITableViewCell() 32 cell.textLabel?.text = "少停" 33 if indexPath.row % 2 == 0{ 34 cell.imageView?.image = UIImage(named: "1111") 35 }else{ 36 cell.imageView?.image = UIImage(named: "2222") 37 } 38 return cell 39 } 40 //section个数 41 func numberOfSectionsInTableView(tableView: UITableView) -> Int { 42 return 3 43 } 44 //页眉 45 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 46 if section == 0{ 47 return "0" 48 }else if section == 1{ 49 return "1" 50 }else{ 51 return "2" 52 } 53 } 54 //页脚 55 func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? { 56 return "..." 57 } 58 //右侧索引 59 func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { 60 return ary 61 } 62 //可以编辑 63 func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 64 if indexPath.section == 1{ 65 return false 66 }else{ 67 return true 68 } 69 } 70 //可以移动 71 func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 72 return true; 73 } 74 //选中 75 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 76 if indexPath.section == 0{ 77 print("选中1区第(indexPath.row)行") 78 }else if indexPath.section == 1 { 79 print("选中2区第(indexPath.row)行") 80 }else{ 81 print("选中3区第(indexPath.row)行") 82 } 83 } 84 85 86 87 override func didReceiveMemoryWarning() { 88 super.didReceiveMemoryWarning() 89 // Dispose of any resources that can be recreated. 90 } 91 92 93 }
UICollectionView:
1 // 2 // ViewController.swift 3 // CollectionView 4 // 5 // Created by shaoting on 16/3/25. 6 // Copyright © 2016年 9elephas. All rights reserved. 7 // 8 9 import UIKit 10 11 12 class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{ 13 var collectionView : UICollectionView? 14 var dataAry = NSMutableArray() 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 let flowLayout = UICollectionViewFlowLayout() 18 flowLayout.itemSize = CGSizeMake(100, 100) 19 flowLayout.scrollDirection = UICollectionViewScrollDirection.Vertical // 设置垂直显示 20 flowLayout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1) //设置边距 21 flowLayout.minimumLineSpacing = 1 //设置相邻layout的上下 22 flowLayout.minimumInteritemSpacing = 1 //设置相邻layout的左右 23 flowLayout.headerReferenceSize = CGSizeMake(0, 0) 24 25 26 collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, self.view.frame.size.width, height: self.view.frame.size.height), collectionViewLayout: flowLayout) 27 collectionView?.delegate = self 28 collectionView?.dataSource = self 29 collectionView?.alwaysBounceVertical = true 30 self.view.addSubview(collectionView!) 31 collectionView?.backgroundColor = UIColor.whiteColor() 32 collectionView?.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell") //注册一个cell 33 34 35 36 37 // Do any additional setup after loading the view, typically from a nib. 38 } 39 40 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ 41 42 return 10 43 } 44 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 45 let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell 46 cell.backgroundColor = UIColor.redColor() 47 48 49 50 return cell 51 } 52 53 54 override func didReceiveMemoryWarning() { 55 super.didReceiveMemoryWarning() 56 // Dispose of any resources that can be recreated. 57 } 58 59 60 }
源码下载地址:
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学习网站;