##一、UICollectionView的簡介
UICollectionView是IOS 6中引進(jìn)的列表展現(xiàn)控件,用于展示集合視圖,布局更加靈活,可以高度定制內(nèi)容的展現(xiàn),可以有效的進(jìn)行數(shù)據(jù)管理,即使對于大量數(shù)據(jù),也非常的高效。蘋果官方給出了Demo([點(diǎn)我下載](https://developer.apple.com/library/ios/samplecode/CollectionView-Simple/Introduction/Intro.html#//apple_ref/doc/uid/DTS40012860))是一個類似于Android里面的GridView的實(shí)現(xiàn)。和UITableView的實(shí)現(xiàn)相比較,他對于每一個Item都是一次復(fù)用,而UITableView只能對于每一行進(jìn)行復(fù)用。如果你認(rèn)為它僅僅是對GridView在IOS中的實(shí)現(xiàn)的話,那你就太小看它的功能了。下面我們就來一起學(xué)習(xí)UICollectionView的使用方法。
##二、UICollectionView的感性認(rèn)識
網(wǎng)上有一個書架的舉例很經(jīng)典,很好的說明了UICollectionView的表現(xiàn)形式。如圖:
一個標(biāo)準(zhǔn)的UICollectionView包含三個部分,他們都是UIView的子類:
- C?ells 用于展示內(nèi)容的主體,對于不同的Cell可以指定不同的尺寸和內(nèi)容。
- Supplementary Views 附加視圖,可以理解為UITableView每個Section的HeaderView和FooterView。
- Decoration Views 裝飾視圖,這是每個section的背景視圖,用于裝飾該section。
將上圖分解為以上三個元素組成的結(jié)構(gòu),如下圖所示:

##三、基本原理
UICollectionView 向 UICollectionViewLayout 詢問布局,當(dāng)詢問過程發(fā)生時,layout 對象會創(chuàng)建 UICollectionViewLayoutAttributes 實(shí)例。一個 UICollectionViewLayoutAttributes 對象管理著一個對應(yīng)的 item layout 相關(guān)信息(一對一關(guān)系)
##四、實(shí)現(xiàn)一個簡單的gridview視圖
效果如下:

###第一步:生成UICollectionViewFlowLayout對象,設(shè)置他的顯示大小、每個item之間的邊距和滾動方向。
```
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/2-10, SCREEN_WIDTH/2-10);
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.minimumLineSpacing = 20;//設(shè)置每個item之間的間距
```
###第二步:生成collectionView對象,設(shè)置他的顯示大小、代理方法和其他相關(guān)屬性。
```
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.navBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-self.navBarHeight) collectionViewLayout:flowLayout];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.showsVerticalScrollIndicator = YES;
collectionView.backgroundColor = [UIColor blackColor];
[self.view addSubview:collectionView];
```
###第三步:注冊Cell,重用Cell
首先在初始化的時候注冊cell的Id。
```
[self.collectionView registerClass:[Demo1Cell class] forCellWithReuseIdentifier:Demo1CellID];
```
然后在使用Cell的時候,使用以下方法進(jìn)行重用。
```
Demo1Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Demo1CellID forIndexPath:indexPath];
```
###第四步:實(shí)現(xiàn)UICollectionViewDataSource的三個代理方法。
- section的數(shù)量 -numberOfSectionsInCollection:
- 某個section里有多少個item-collectionView:numberOfItemsInSection:
- 對于某個位置應(yīng)該顯示什么樣的cell -collectionView:cellForItemAtIndexPath:
```
#pragma mark- UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 31;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
Demo1Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Demo1CellID forIndexPath:indexPath];
if(!cell){
cell = [[Demo1Cell alloc] init];
}
[cell setImageName:[NSString stringWithFormat:@"%zi",indexPath.row] content:[NSString stringWithFormat:@"{%zi,%zi}",indexPath.section,indexPath.row]];
return cell;
}
```
##五、Demo下載地址
[?Demo下載地址](https://github.com/yixiangboy/YXCollectionView.git)
如果對你有點(diǎn)幫助,star一下吧。
##六、聯(lián)系方式
微博:[新浪微博](http://weibo.com/5612984599/profile?topnav=1&wvr=6)
博客:[http://blog.csdn.net/yixiangboy ](http://blog.csdn.net/yixiangboy)
github:[https://github.com/yixiangboy](https://github.com/yixiangboy)