UIKit框架-高级控件Swift版本: 4.UICollectionView方法/属性详解

前端之家收集整理的这篇文章主要介绍了UIKit框架-高级控件Swift版本: 4.UICollectionView方法/属性详解前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前面我们讲解完了iOS的第一个列表控件UITableView以及它的表格空间UITableViewCell,这次让我们继续来讲解iOS的第二个列表控件UICollectionView.


1.UICollectionView的常用属性

  1. // 1.设置位置和大小
  2. init(frame: CGRect,collectionViewLayout layout: UICollectionViewLayout)
  3.  
  4. // 2.设置子视图的布局方式
  5. var collectionViewLayout: UICollectionViewLayout
  6.  
  7. // 3.设置UICollectionView的代理对象
  8. unowned(unsafe) var delegate: UICollectionViewDelegate?
  9.  
  10. // 4.设置UICollectionView的数据源对象
  11. unowned(unsafe) var dataSource: UICollectionViewDataSource?
  12.  
  13. // 5.设置UICollectionView的背景视图
  14. var backgroundView: UIView?
  15.  
  16. // 6.设置 UICollectionView 的 Cell 是否可以点击
  17. var allowsSelection: Bool
  18.  
  19. // 7.设置 UICollectionView 的 Cell 是否可以多选
  20. var allowsMultipleSelection: Bool

UICollectionViewCell显示的样式

  1. struct UICollectionViewScrollPosition : RawOptionSetType {
  2. init(_ rawValue: UInt)
  3. init(rawValue: UInt)
  4.  
  5. // 1.没有样式
  6. static var None: UICollectionViewScrollPosition { get }
  7.  
  8. // 2.垂直居中显示
  9. static var CenteredVertically: UICollectionViewScrollPosition { get }
  10.  
  11. // 3.向下显示
  12. static var Bottom: UICollectionViewScrollPosition { get }
  13.  
  14. // 4.向左显示
  15. static var Left: UICollectionViewScrollPosition { get }
  16.  
  17. // 5.水平居中显示
  18. static var CenteredHorizontally: UICollectionViewScrollPosition { get }
  19.  
  20. // 6.向右显示
  21. static var Right: UICollectionViewScrollPosition { get }
  22. }

2.UICollectionView常用的方法

  1. // 1.设置UICollectionView的注册类,以及标示符
  2. func registerClass(cellClass: AnyClass?,forCellWithReuseIdentifier identifier: String)
  3.  
  4. // 2.设置 UICollectionView的注册Nib,以及标示符
  5. func registerNib(nib: UINib?,forCellWithReuseIdentifier identifier: String)
  6.  
  7. // 3.设置 UICollectionView 的注册类,以及辅助视图名称,标示符
  8. func registerClass(viewClass: AnyClass?,forSupplementaryViewOfKind elementKind: String,withReuseIdentifier identifier: String)
  9.  
  10. // 4.设置 UICollectionView的注册Nib,标示符
  11. func registerNib(nib: UINib?,forSupplementaryViewOfKind kind: String,withReuseIdentifier identifier: String)
  12.  
  13. // 5.设置 UICollectionView 可重用的 Cell 以及所以路径
  14. func dequeueReusableCellWithReuseIdentifier(identifier: String,forIndexPath indexPath: NSIndexPath!) -> AnyObject
  15.  
  16. // 6.设置 UICollectionView 可重用的的辅视图,标示符,以及索引路径
  17. func dequeueReusableSupplementaryViewOfKind(elementKind: String,withReuseIdentifier identifier: String,forIndexPath indexPath: NSIndexPath!) -> AnyObject
  18.  
  19. // 7.选择 Item 的索引路径
  20. func indexPathsForSelectedItems() -> [AnyObject]
  21.  
  22. // 8.选择 Item 的索引路径,以及是否使用动画,显示样式
  23. func selectItemAtIndexPath(indexPath: NSIndexPath?,animated: Bool,scrollPosition: UICollectionViewScrollPosition)
  24.  
  25. // 9.取消选择 Item 的索引路径,以及是否使用动画
  26. func deselectItemAtIndexPath(indexPath: NSIndexPath?,animated: Bool)
  27.  
  28. // 10.刷新数据
  29. func reloadData()
  30.  
  31. // 11.设置 UICollectionView 的集合视图布局,及是否使用动画
  32. func setCollectionViewLayout(layout: UICollectionViewLayout,animated: Bool)
  33.  
  34. // 12.设置 UICollectionView 的集合视图布局,及是否使用动画,以及完成之后的闭包方法
  35. func setCollectionViewLayout(layout: UICollectionViewLayout,completion: ((Bool) -> Void)!)
  36.  
  37. // 13.设置 UICollectionView 显示多少个 Item
  38. func numberOfSections() -> Int
  39.  
  40. // 14.设置 UICollectionView 显示多少组 Item
  41. func numberOfItemsInSection(section: Int) -> Int
  42.  
  43. // 15.设置 UICollectionView 滚动到第几个 Item 的索引路径,以及显示样式和是否启用动画
  44. func scrollToItemAtIndexPath(indexPath: NSIndexPath,atScrollPosition scrollPosition: UICollectionViewScrollPosition,animated: Bool)
  45.  
  46. // 16.在 UICollectionView 中插入某个 Item
  47. func insertSections(sections: NSIndexSet)
  48.  
  49. // 17.在 UICollectionView 中删除某个 Item
  50. func deleteSections(sections: NSIndexSet)
  51.  
  52. // 16.在 UICollectionView 中刷新某个 Item
  53. func reloadSections(sections: NSIndexSet)
  54.  
  55. // 17.移动 UICollectionView 中某个 Item 到某个位置
  56. func moveSection(section: Int,toSection newSection: Int)

UICollectionView代理方法

  1. // 1.点击 Item 时调用方法
  2. optional func collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath)
  3.  
  4. // 2.取消选中 Item 时调用方法
  5. optional func collectionView(collectionView: UICollectionView,didDeselectItemAtIndexPath indexPath: NSIndexPath)

UICollectionView数据源方法

  1. // 1.设置UICollectionView有多少个Item
  2. func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int
  3.  
  4. // 2.设置 UICollectionViewCell 所显示内容,以及索引路径
  5. func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
  6.  
  7. // 3.设置 UICollectionView 有多少组 Cell
  8. optional func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int

UICollectionView的集视图布局方法

  1. // 1.方法是用来设置 UICollectionView 的 Item 尺寸大小
  2. optional func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
  3.  
  4. // 2.方法是用来设置 UICollectionView 的 Item 四周的边界
  5. optional func collectionView(collectionView: UICollectionView,insetForSectionAtIndex section: Int) -> UIEdgeInsets
  6.  
  7. // 3.方法是用来设置 UICollectionView 的 Item 上下之间的最小间距(如果在自定义UICollectionView中实现了该属性,那么该方法就会覆盖掉原来的属性)
  8. optional func collectionView(collectionView: UICollectionView,minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
  9.  
  10. // 4.方法是用来设置 UICollectionView 的 Item 左右之间的最小间距(如果在自定义UICollectionView中实现了该属性,minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
  11.  
  12. // 5.方法是用来设置 UICollectionView 的页头尺寸(如果在自定义UICollectionView中实现了该属性,referenceSizeForHeaderInSection section: Int) -> CGSize
  13.  
  14. // 6.方法是用来设置 UIcollectionView 的页尾尺寸(如果在自定义UICollectionView中实现了该属性,referenceSizeForFooterInSection section: Int) -> CGSize

3.代码演示

首先我们要遵守以下协议

  1. class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
  2. }

自定义UICollectionView

  1. func myCollectionView() {
  2. // 1.自定义 Item 的FlowLayout
  3. let flowLayout = UICollectionViewFlowLayout()
  4.  
  5. // 2.设置 Item 的 Size
  6. flowLayout.itemSize = CGSizeMake(90,120)
  7.  
  8. // 3.设置 Item 的排列方式
  9. flowLayout.scrollDirection = UICollectionViewScrollDirection.Vertical
  10.  
  11. // 4.设置 Item 的四周边距
  12. flowLayout.sectionInset = UIEdgeInsetsMake(20,20,20)
  13.  
  14. // 5.设置同一竖中上下相邻的两个 Item 之间的间距
  15. flowLayout.minimumLineSpacing = 20
  16.  
  17. // 6.设置同一行中相邻的两个 Item 之间的间距
  18. flowLayout.minimumInteritemSpacing = 20
  19.  
  20. // 7.设置UICollectionView 的页头尺寸
  21. flowLayout.headerReferenceSize = CGSizeMake(100,50)
  22.  
  23. // 8.设置 UICollectionView 的页尾尺寸
  24. flowLayout.footerReferenceSize = CGSizeMake(100,50)
  25.  
  26. // 1.自定义 UICollectionView 的位置大小,以及 Item 的显示样式为 flowLayout
  27. var collection = UICollectionView(frame: CGRectMake(0,64,self.view.frame.width,self.view.frame.height - 64),collectionViewLayout: flowLayout)
  28.  
  29. // 2.设置 UICollectionView 的背景颜色
  30. collection.backgroundColor = UIColor.whiteColor()
  31.  
  32. // 3.设置 UICollectionView 垂直滚动是否滚到 Item 的最底部内容
  33. collection.alwaysBounceVertical = true
  34.  
  35. // 4.设置 UICollectionView 垂直滚动是否滚到 Item 的最右边内容
  36. collection.alwaysBounceHorizontal = true
  37.  
  38. // 5.设置 UICollectionView 的数据源对象
  39. collection.dataSource = self
  40.  
  41. // 6.设置 UICollectionView 的代理对象
  42. collection.delegate = self
  43.  
  44. // 7.设置 UICollectionView 的单元格点击(默认是 true)
  45. collection.allowsSelection = true
  46.  
  47. // 8.设置 UICollectionView 的单元格多选(默认是 false)
  48. collection.allowsMultipleSelection = false
  49.  
  50. // 9.开启 UICollectionView 的分页显示效果
  51. collection.pagingEnabled = true
  52.  
  53. // 10.注册 UICollectionViewCell
  54. collection.registerClass(UICollectionViewCell.self,forCellWithReuseIdentifier: "cell")
  55. // 11.添加到 self.view 上
  56. self.view.addSubview(collection)
  57. }

自定义UINavigationBar

  1. func myNavigationBar() {
  2. // 1.自定义 NavigationBar,设置它的位置大小
  3. var navigationBar = UINavigationBar(frame: CGRectMake(0,0,self.view.frame.width,64))
  4. // 2.设置 NavigationBar 的背景色
  5. navigationBar.backgroundColor = UIColor.redColor()
  6. // 3.自定义 NavigationItem 设定它的 Title
  7. let navigationItem = UINavigationItem(title: "UICollectionView演示")
  8. // 4.自定义 UIBarButtonItem 的Title,Style,Target 的对象,已经监听的方法
  9. let leftButton = UIBarButtonItem(title: "返回",style: UIBarButtonItemStyle.Plain,target: self,action: "back")
  10. // 5.设置 Navigation 左边的按钮为 leftButton
  11. navigationItem.leftBarButtonItem = leftButton
  12. // 6.把 NavigationItem 添加到 NavigationBar
  13. navigationBar.pushNavigationItem(navigationItem,animated: true)
  14. // 7.添加到到 self.view 上
  15. self.view.addSubview(navigationBar)
  16. }
  17.  
  18. // 8.NavigationBar监听方法
  19. func back() {
  20. println("点击了返回")
  21. }

UICollectionView的代理方法,数据源方法,FlowLayout 方法

  1. // 1.方法是用来设置返回 CollectionViewCell 的组数
  2. func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
  3. return 1
  4. }
  5.  
  6. // 2.方法是用来设置返回 CollectionViewCell 的个数
  7. func collectionView(collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int {
  8. return 15
  9. }
  10.  
  11. // 3.方法是用来设置 CollectionViewCell 的内容
  12. func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  13. var collectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell",forIndexPath: indexPath) as! UICollectionViewCell
  14. collectionCell.backgroundColor = UIColor.redColor()
  15.  
  16. return collectionCell
  17. }
  18.  
  19. // 4.方法是点击了 CollectionViewCell 时调用的监听方法
  20. func collectionView(collectionView: UICollectionView,didSelectItemAtIndexPath indexPath: NSIndexPath) {
  21. println("aaa")
  22. }
  23.  
  24. // 5.方法是用来设置 CollectionViewCell 的大小
  25. func collectionView(collectionView: UICollectionView,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
  26. return CGSizeMake(90,120)
  27. }
  28.  
  29. // 6.方法是用来设置 CollectionViewCell 四周的边距
  30. func collectionView(collectionView: UICollectionView,insetForSectionAtIndex section: Int) -> UIEdgeInsets {
  31. return UIEdgeInsetsMake(20,20)
  32. }
  33.  
  34. // 7.方法是用来设置同一行 CollectionViewCell 之间的间距
  35. func collectionView(collectionView: UICollectionView,minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
  36. return 20
  37. }
  38.  
  39. // 8.方法是用来设置同一列 CollectionViewCell 之间的间距
  40. func collectionView(collectionView: UICollectionView,minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
  41. return 20
  42. }
  43.  
  44. // 9.方法是用来设置 CollectionView 的页头尺寸
  45. func collectionView(collectionView: UICollectionView,referenceSizeForHeaderInSection section: Int) -> CGSize {
  46. return CGSizeMake(100,50)
  47. }
  48.  
  49. // 10.方法是用来设置 CollectionView 的页尾尺寸
  50. func collectionView(collectionView: UICollectionView,referenceSizeForFooterInSection section: Int) -> CGSize {
  51. return CGSizeMake(100,50)
  52. }

4.最终效果

PS: UIColleCtionView 是继承于 UIScrollView 的,所以 UIScrollView 里的属性,以及方法都是可以用的.


好了这次我们就讲到这里,下次我们继续~~

猜你在找的Swift相关文章