在今天這篇博客中咱們要接着上篇博客中的Demo,使其自定義佈局的屬性在使用它的UICollectionView中是可配置的。 固然在本篇要介紹的Demo中只提取了四個佈局參數,不管添加一個Delegate,但思路都是同樣的。咱們把上一篇博客中寫死的內容,經過佈局代理來提供參數配置,這樣就靈活多了。好了,鹹淡扯的適中,進入咱們今天的主題。git
下圖算是Demo的2.0版本的運行效果,和以前的比較起來功能確實強大了很多。由於他是可配置化的,根據用戶輸入的參數來肯定瀑布流的樣式。固然Demo中是經過用戶輸入的參數來肯定的,若是你在代碼中使用該自定義瀑布流,須要根據你的實際狀況能夠配置瀑布流的參數,來打造屬於你本身的瀑布流。網上雖然好多實現瀑布流的博客和代碼,可是像今天這樣可配置的瀑布流應該是很少的,至少我沒見過,因此嘍就寫一個,開源一下,給你們分享交流一下。github
該自定義瀑布流佈局的使用方式和系統自帶的UICollectionViewDelegateFlowLayout
用法一直,都是經過佈局代理來定製佈局參數,關於UICollectionViewDelegateFlowLayout
的內容詳見《iOS開發之窺探UICollectionViewController(二) --詳解CollectionView各類回調》中有關UICollectionViewDelegateFlowLayout
代理介紹的內容。面試
若是想使用該佈局文件,你須要爲咱們的UICollectionView來指定該佈局文件,在本篇博客中的Demo中是在Storyboard中進行自定義佈局文件的指定的,你也能夠經過代碼的方式指定,再次不作過多的贅述。指定該自定義佈局後,你須要作如下事情:bash
首先獲取UICollectionView的佈局collectionViewLayout,而後爲其設置CustomeCollectionViewLayoutDelegate代理便可,代碼以下:佈局
1 _customeLayout = (CustomeCollectionViewLayout *) self.collectionViewLayout;
2
3 _customeLayout.layoutDelegate = self;
複製代碼
須要在UICollectionView的使用控制器中實現自定義佈局中的代理方法來設置佈局屬性,咱們這兒定了四個必須實現的方法。 你能夠經過這些方法去設定cell的列數,Cell的外邊距,Cell的最小高度,Cell的最大高度,以下所示:post
#pragma mark <CustomeCollectionViewLayoutDelegate>
- (NSInteger) numberOfColumnWithCollectionView: (UICollectionView *)collectionView
collectionViewLayout:( CustomeCollectionViewLayout *)collectionViewLayout{
return _cellColumn;
}
- (CGFloat)marginOfCellWithCollectionView:(UICollectionView *)collectionView collectionViewLayout:(CustomeCollectionViewLayout *)collectionViewLayout{
return _cellMargin;
}
- (CGFloat)minHeightOfCellWithCollectionView:(UICollectionView *)collectionView collectionViewLayout:(CustomeCollectionViewLayout *)collectionViewLayout{
return _cellMinHeight;
}
- (CGFloat)maxHeightOfCellWithCollectionView:(UICollectionView *)collectionView collectionViewLayout:(CustomeCollectionViewLayout *)collectionViewLayout{
return _cellMaxHeight;
}
複製代碼
至此,咱們的自定義瀑布流就相對比較完善了,不過還有好大的改善控件。感興趣的小夥伴能夠在此基礎上加上你本身的東西。ui
本篇博客中Demo的github分享地址爲:github.com/lizelu/Cust…spa
你認爲如何?請經過加咱們的交流羣 點擊此處進交流羣 ,來一塊兒交流或者發佈您的問題,意見或反饋。
做者:青玉伏案 出處:www.cnblogs.com/ludashi/代理