1.基本介紹
UICollectionView的基礎(chǔ)是UITableView, UITableView 大家肯定用得很熟悉,UITableView中的只支持單排列表,沒有辦法支持網(wǎng)格列表,在IOS6 中就出了UICollectionView,UICollectionView能更好的支持網(wǎng)格實(shí)圖
2.使用方法
UICollectionView與UITableView基本相似,不同的地方就是UICollectionViewFlowLayout,UICollectionView的delegate和UITableView基本相似。下面來說一下不同的地方
(1)UICollectionViewFlowLayout
a. UICollectionViewFlowLayout的屬性
UICollectionViewFlowLayout可以用屬性設(shè)置UICollectionViewCell顯示的大小,間隔,排列方式等。

b. UICollectionViewFlowLayout的代理
當(dāng)然你也可以設(shè)置利用代理方法UICollectionViewDelegateFlowLayout,
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath;

每一個(gè)cell的大小
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

//設(shè)置每組的cell的邊界,
- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;

//cell的最小行間距
- (CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

//cell的最小列間距
c. UICollectionViewLayout是一個(gè)抽象的類型,所有在使用的時(shí)候可以使用它的子類UICollectionViewFlowLayout(IOS提供),或者你也可以選擇重寫一個(gè)UICollectionViewLayout,下面來簡單說一下UICollectionViewLayout
- (CGSize)collectionViewContentSize; //返回內(nèi)容尺寸

返回自定義的內(nèi)容尺寸(必須實(shí)現(xiàn))
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;//返回對(duì)應(yīng)于indexPath的位置的cell的布局屬性
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;//返回rect中的所有的元素的布局屬性,返回的是包含UICollectionViewLayoutAttributes的NSArray
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds //當(dāng)邊界發(fā)生改變時(shí),是否應(yīng)該刷新布局。如果YES則在邊界變化(一般是scroll到其他地方)時(shí),將重新計(jì)算需要的布局信息
在UICollectionViewLayout里面,首先-(void)prepareLayout將被調(diào)用,之后-(CGSize) collectionViewContentSize將被調(diào)用,接著-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect被調(diào)用
作者:LeoDavid
鏈接:http://www.itdecent.cn/p/f320a6fa6407
來源:簡書
簡書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。