(swift)UICollectionView方法/屬性詳解

文章鏈接 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 里的屬性, 以及方法都是可以用的.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市捞魁,隨后出現(xiàn)的幾起案子官卡,更是在濱河造成了極大的恐慌淤翔,老刑警劉巖使套,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件濒蒋,死亡現(xiàn)場離奇詭異裆馒,居然都是意外死亡霜第,警方通過查閱死者的電腦和手機蹦掐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門技羔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來僵闯,“玉大人,你說我怎么就攤上這事藤滥”钏冢” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵拙绊,是天一觀的道長向图。 經(jīng)常有香客問我,道長标沪,這世上最難降的妖魔是什么榄攀? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮谨娜,結(jié)果婚禮上航攒,老公的妹妹穿的比我還像新娘。我一直安慰自己趴梢,他們只是感情好漠畜,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著坞靶,像睡著了一般憔狞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上彰阴,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天瘾敢,我揣著相機與錄音,去河邊找鬼尿这。 笑死簇抵,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的射众。 我是一名探鬼主播碟摆,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼叨橱!你這毒婦竟也來了典蜕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤罗洗,失蹤者是張志新(化名)和其女友劉穎愉舔,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體伙菜,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡轩缤,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片典奉。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡躺翻,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出卫玖,到底是詐尸還是另有隱情公你,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布假瞬,位于F島的核電站陕靠,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏脱茉。R本人自食惡果不足惜剪芥,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望琴许。 院中可真熱鬧税肪,春花似錦、人聲如沸榜田。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽箭券。三九已至净捅,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間辩块,已是汗流浹背蛔六。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留废亭,地道東北人国章。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像豆村,于是被迫代替她去往敵國和親捉腥。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容