UICollectionView 筆記
Collection View Basics
CollectionView和tableView有幾分相似卻又有不同。
那什么是collectionView呢信认。你看過蘋果自帶的相冊嗎那個(gè)就是collectionView贸桶。就是一個(gè)個(gè)cell組成的展示的容器
組成
首先要理解一下他的組成,并與tableView進(jìn)行比較已维。
Delegate DataSource
它和tableView一樣,都是由delegate和dataSource驅(qū)動(dòng)的。它需要的協(xié)議是,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout则拷。而這部分和tableView一樣是負(fù)責(zé)定義cell和監(jiān)聽cell的一系列的問題。CollectionView layout
這部分是tableView 所沒有的曹鸠,這部分是復(fù)雜cell的布局煌茬。例如是水平展示還是垂直,每個(gè)Cell間隔多少...... Something like that性質(zhì)方面
在CollectionView中它和tableView不一樣彻桃,tableView是一排垂直的東西坛善,就那么直直的展示下來,而CollectionView是一個(gè)個(gè)的小cell(細(xì)胞)邻眷。
![Uploading ds_data_object_layout_2x_766033.png . . .]
Designing Your Data Source and Delegate
存放數(shù)據(jù)的容器結(jié)構(gòu)
第一層是一個(gè)NSMutableArray里面 add 例外一些 NSMutableArray的數(shù)組(這就是第二層數(shù)據(jù))眠屎。第3層才是存放正真的數(shù)據(jù)。如圖肆饶。
Data Source and Delegate
定義 一共有多少的大section
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
定義 每個(gè)section中有多少個(gè)cell(item)
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section]
定義每個(gè)UICollectionView 的大小 (可以不用代理寫)
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
定義每個(gè)UICollectionView 的 margin(內(nèi)邊距)(可以不用代理寫)
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
UICollectionView被選中時(shí)調(diào)用的方法 相當(dāng)于 tableView didSelect
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
這個(gè)是最重要的方法组力,定義了每個(gè)cell要現(xiàn)實(shí)什么數(shù)據(jù)
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
創(chuàng)建 Collection View
創(chuàng)建當(dāng)然是用alloc啦,來看看
self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
然后就看蒙了抖拴,collectionViewLayout是什么鬼燎字。還記得之前介紹的layout嗎,這個(gè)就是阿宅。
然后就是在alloc之前 先創(chuàng)建一個(gè) collectionViewLayout
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
這個(gè)layout里面還是有幾個(gè)數(shù)據(jù)要介紹一下的候衍。
minimumLineSpacing //The minimum spacing to use between lines of items in the grid.
也就是cell與cell的上下之間的最小間距。 CGFloat
minimumInteritemSpacing //The minimum spacing to use between items in the same row.
cell與cell之間的橫向最小間距洒放。 CGFloat
itemSize //The default size to use for cells.
cell的size CGSize
estimatedItemSize //The estimated size of cells in the collection view.
先給collectionViewCell先估算個(gè)一個(gè)高度,然后在代理方法中再返回真實(shí)的size
sectionInset //The margins used to lay out content in a section
這個(gè)就是設(shè)置cell與cell的內(nèi)邊距的東西 UIEdgeInsets
創(chuàng)建總結(jié)
也就是第一步 你要?jiǎng)?chuàng)建個(gè) layout對(duì)象蛉鹿,賦予一些 基本的值。然后再alloc一個(gè)Collection View往湿。然后設(shè)置delegate等妖异。惋戏。。
自定義cell
alloc好了Collection View他膳,那么就是要自定義一些cell去show數(shù)據(jù)响逢。和tableView不同的是這里要多一個(gè)步驟。例如的我 UICollectionViewCell的子類的名字叫 CollectionViewTitleCell棕孙。那么就要如下:
//注冊子cell
[_collectionView registerClass:[VSTitleOfSecondCategoryCell class] forCellWithReuseIdentifier:@"CollectionViewTitleCell"];
//然后在cellForItemAtIndexPath中這么寫
//取緩存并強(qiáng)制轉(zhuǎn)換
VSTitleOfSecondCategoryCell *cell = (VSTitleOfSecondCategoryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
然后就可以給cell 賦值啊定義的啊........
參考致謝
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CollectionViewBasics/CollectionViewBasics.html#//apple_ref/doc/uid/TP40012334-CH2-SW1
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewFlowLayout_class/index.html#//apple_ref/occ/instp/UICollectionViewFlowLayout/sectionInset