文章鏈接 blog.csdn.net/sinat_34194127/article/details/51818975
blog.csdn.net/sinat_34194127/article/details/51818975
1.UICollectionView的常用屬性
// 1.設(shè)置位置和大小init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout)
// 2.設(shè)置子視圖的布局方式varcollectionViewLayout: UICollectionViewLayout
// 3.設(shè)置UICollectionView的代理對象unowned(unsafe)vardelegate: UICollectionViewDelegate?
// 4.設(shè)置UICollectionView的數(shù)據(jù)源對象unowned(unsafe)vardataSource: UICollectionViewDataSource?
// 5.設(shè)置UICollectionView的背景視圖varbackgroundView: UIView?
// 6.設(shè)置 UICollectionView 的 Cell 是否可以點擊varallowsSelection: Bool
// 7.設(shè)置 UICollectionView 的 Cell 是否可以多選varallowsMultipleSelection: Bool
UICollectionViewCell顯示的樣式
structUICollectionViewScrollPosition : RawOptionSetType {? ? init(_ rawValue: UInt)? ? init(rawValue: UInt)
// 1.沒有樣式staticvarNone: UICollectionViewScrollPosition {get}
// 2.垂直居中顯示staticvarCenteredVertically: UICollectionViewScrollPosition {get}
// 3.向下顯示staticvarBottom: UICollectionViewScrollPosition {get}
// 4.向左顯示staticvarLeft: UICollectionViewScrollPosition {get}
// 5.水平居中顯示staticvarCenteredHorizontally: UICollectionViewScrollPosition {get}
// 6.向右顯示staticvarRight: UICollectionViewScrollPosition {get} }
2.UICollectionView常用的方法
// 1.設(shè)置UICollectionView的注冊類, 以及標示符funcregisterClass(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
// 2.設(shè)置 UICollectionView的注冊Nib, 以及標示符funcregisterNib(nib: UINib?, forCellWithReuseIdentifier identifier: String)
// 3.設(shè)置 UICollectionView 的注冊類, 以及輔助視圖名稱, 標示符funcregisterClass(viewClass: AnyClass?, forSupplementaryViewOfKind elementKind: String, withReuseIdentifier identifier: String)
// 4.設(shè)置 UICollectionView的注冊Nib, 以及輔助視圖名稱, 標示符funcregisterNib(nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)
// 5.設(shè)置 UICollectionView 可重用的 Cell 以及所以路徑funcdequeueReusableCellWithReuseIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath!)-> AnyObject
// 6.設(shè)置 UICollectionView 可重用的的輔視圖, 標示符, 以及索引路徑funcdequeueReusableSupplementaryViewOfKind(elementKind: String, withReuseIdentifier identifier: String, forIndexPath indexPath: NSIndexPath!)-> AnyObject
// 7.選擇 Item 的索引路徑funcindexPathsForSelectedItems()->[AnyObject]
// 8.選擇 Item 的索引路徑, 以及是否使用動畫, 顯示樣式funcselectItemAtIndexPath(indexPath: NSIndexPath?, animated: Bool, scrollPosition: UICollectionViewScrollPosition)
// 9.取消選擇 Item 的索引路徑, 以及是否使用動畫funcdeselectItemAtIndexPath(indexPath: NSIndexPath?, animated: Bool)
// 10.刷新數(shù)據(jù)funcreloadData()
// 11.設(shè)置 UICollectionView 的集合視圖布局, 及是否使用動畫funcsetCollectionViewLayout(layout: UICollectionViewLayout, animated: Bool)
// 12.設(shè)置 UICollectionView 的集合視圖布局, 及是否使用動畫, 以及完成之后的閉包方法funcsetCollectionViewLayout(layout: UICollectionViewLayout, animated: Bool, completion: ((Bool)-> Void)!)
// 13.設(shè)置 UICollectionView 顯示多少個 ItemfuncnumberOfSections()-> Int
// 14.設(shè)置 UICollectionView 顯示多少組 ItemfuncnumberOfItemsInSection(section: Int)-> Int
// 15.設(shè)置 UICollectionView 滾動到第幾個 Item 的索引路徑, 以及顯示樣式和是否啟用動畫funcscrollToItemAtIndexPath(indexPath: NSIndexPath, atScrollPosition scrollPosition: UICollectionViewScrollPosition, animated: Bool)
// 16.在 UICollectionView 中插入某個 ItemfuncinsertSections(sections: NSIndexSet)
// 17.在 UICollectionView 中刪除某個 ItemfuncdeleteSections(sections: NSIndexSet)
// 16.在 UICollectionView 中刷新某個 ItemfuncreloadSections(sections: NSIndexSet)
// 17.移動 UICollectionView 中某個 Item 到某個位置funcmoveSection(section: Int, toSection newSection: Int)
UICollectionView代理方法
// 1.點擊 Item 時調(diào)用的方法optional func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath:NSIndexPath)
// 2.取消選中 Item 時調(diào)用的方法optional func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath:NSIndexPath)
UICollectionView數(shù)據(jù)源方法
//1.設(shè)置UICollectionView有多少個Item? ? func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int)->Int
//2.設(shè)置 UICollectionViewCell 所顯示的內(nèi)容, 以及索引路徑? ? func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)->UICollectionViewCell
//3.設(shè)置 UICollectionView 有多少組 Cell? ? optional func numberOfSectionsInCollectionView(collectionView: UICollectionView)->Int
UICollectionView的集視圖布局方法
//1.該方法是用來設(shè)置 UICollectionView 的 Item 尺寸大小? ? optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath)->CGSize
//2.該方法是用來設(shè)置 UICollectionView 的 Item 四周的邊界? ? optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int)->UIEdgeInsets
//3.該方法是用來設(shè)置 UICollectionView 的 Item 上下之間的最小間距(如果在自定義UICollectionView中實現(xiàn)了該屬性, 那么該方法就會覆蓋掉原來的屬性)? ? optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int)->CGFloat
//4.該方法是用來設(shè)置 UICollectionView 的 Item 左右之間的最小間距(如果在自定義UICollectionView中實現(xiàn)了該屬性, 那么該方法就會覆蓋掉原來的屬性)? ? optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int)->CGFloat
//5.該方法是用來設(shè)置 UICollectionView 的頁頭尺寸(如果在自定義UICollectionView中實現(xiàn)了該屬性, 那么該方法就會覆蓋掉原來的屬性)? ? optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int)->CGSize
//6.該方法是用來設(shè)置 UIcollectionView 的頁尾尺寸(如果在自定義UICollectionView中實現(xiàn)了該屬性, 那么該方法就會覆蓋掉原來的屬性)? ? optional func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int)->CGSize
3.代碼演示
首先我們要遵守以下協(xié)議
classViewController:UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{}
自定義UICollectionView
func myCollectionView() {
// 1.自定義 Item 的FlowLayoutletflowLayout=UICollectionViewFlowLayout()
// 2.設(shè)置 Item 的 SizeflowLayout.itemSize=CGSizeMake(90,120)
// 3.設(shè)置 Item 的排列方式flowLayout.scrollDirection=UICollectionViewScrollDirection.Vertical
// 4.設(shè)置 Item 的四周邊距flowLayout.sectionInset=UIEdgeInsetsMake(20,20,20,20)
// 5.設(shè)置同一豎中上下相鄰的兩個 Item 之間的間距flowLayout.minimumLineSpacing=20
// 6.設(shè)置同一行中相鄰的兩個 Item 之間的間距flowLayout.minimumInteritemSpacing=20
// 7.設(shè)置UICollectionView 的頁頭尺寸flowLayout.headerReferenceSize=CGSizeMake(100,50)
// 8.設(shè)置 UICollectionView 的頁尾尺寸flowLayout.footerReferenceSize=CGSizeMake(100,50)// 1.自定義 UICollectionView 的位置大小, 以及 Item 的顯示樣式為 flowLayoutvarcollection=UICollectionView(frame: CGRectMake(0,64,self.view.frame.width,self.view.frame.height-64), collectionViewLayout: flowLayout)// 2.設(shè)置 UICollectionView 的背景顏色collection.backgroundColor=UIColor.whiteColor()// 3.設(shè)置 UICollectionView 垂直滾動是否滾到 Item 的最底部內(nèi)容collection.alwaysBounceVertical=true// 4.設(shè)置 UICollectionView 垂直滾動是否滾到 Item 的最右邊內(nèi)容collection.alwaysBounceHorizontal=true// 5.設(shè)置 UICollectionView 的數(shù)據(jù)源對象collection.dataSource=self// 6.設(shè)置 UICollectionView 的代理對象collection.delegate=self// 7.設(shè)置 UICollectionView 的單元格點擊(默認是 true)collection.allowsSelection=true// 8.設(shè)置 UICollectionView 的單元格多選(默認是 false)collection.allowsMultipleSelection=false// 9.開啟 UICollectionView 的分頁顯示效果collection.pagingEnabled=true// 10.注冊 UICollectionViewCellcollection.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier:"cell")// 11.添加到 self.view 上self.view.addSubview(collection)? ? }
自定義UINavigationBar
func myNavigationBar() {// 1.自定義 NavigationBar, 設(shè)置它的位置大小var navigationBar =UINavigationBar(frame: CGRectMake(0,0,self.view.frame.width,64))// 2.設(shè)置 NavigationBar 的背景色navigationBar.backgroundColor=UIColor.redColor()// 3.自定義 NavigationItem 設(shè)定它的 Titlelet navigationItem = UINavigationItem(title:"UICollectionView演示")// 4.自定義 UIBarButtonItem 的Title, Style, Target 的對象, 已經(jīng)監(jiān)聽的方法let leftButton =UIBarButtonItem(title:"返回", style: UIBarButtonItemStyle.Plain, target:self, action:"back")// 5.設(shè)置 Navigation 左邊的按鈕為 leftButtonnavigationItem.leftBarButtonItem= leftButton// 6.把 NavigationItem 添加到 NavigationBarnavigationBar.pushNavigationItem(navigationItem, animated:true)// 7.添加到到 self.view 上self.view.addSubview(navigationBar)? ? }// 8.NavigationBar監(jiān)聽方法func back() {? ? ? ? println("點擊了返回")? ? }
UICollectionView的代理方法, 數(shù)據(jù)源方法, FlowLayout 方法
//1.該方法是用來設(shè)置返回 CollectionViewCell 的組數(shù)? ? func numberOfSectionsInCollectionView(collectionView: UICollectionView)->Int {return1}//2.該方法是用來設(shè)置返回 CollectionViewCell 的個數(shù)? ? func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int)->Int {return15}//3.該方法是用來設(shè)置 CollectionViewCell 的內(nèi)容? ? func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)->UICollectionViewCell {varcollectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell",forIndexPath: indexPath) as! UICollectionViewCell? ? ? ? collectionCell.backgroundColor = UIColor.redColor()returncollectionCell? ? }//4.該方法是點擊了 CollectionViewCell 時調(diào)用的監(jiān)聽方法? ? func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPathindexPath: NSIndexPath) {? ? ? ? println("aaa")? ? }//5.該方法是用來設(shè)置 CollectionViewCell 的大小? ? func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath)->CGSize {returnCGSizeMake(90,120)? ? }//6.該方法是用來設(shè)置 CollectionViewCell 四周的邊距? ? func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int)->UIEdgeInsets {returnUIEdgeInsetsMake(20,20,20,20)? ? }//7.該方法是用來設(shè)置同一行 CollectionViewCell 之間的間距? ? func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int)->CGFloat {return20}//8.該方法是用來設(shè)置同一列 CollectionViewCell 之間的間距? ? func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int)->CGFloat {return20}//9.該方法是用來設(shè)置 CollectionView 的頁頭尺寸? ? func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int)->CGSize {returnCGSizeMake(100,50)? ? }//10.該方法是用來設(shè)置 CollectionView 的頁尾尺寸? ? func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int)->CGSize {returnCGSizeMake(100,50)? ? }
4.最終效果
PS: UIColleCtionView 是繼承于 UIScrollView 的, 所以 UIScrollView 里的屬性, 以及方法都是可以用的.