IOS UICollectionView基礎+UICollectionViewFlowLayout基礎

来源:http://www.cnblogs.com/xubaoaichiyu/archive/2016/03/19/5294382.html
-Advertisement-
Play Games

UICollectionView在眾多控制項中也算是比較常用的了,像淘寶在瀏覽寶貝時採用的就是UICollectionView,對於UICollectionView->UICollectionViewFlowLayout當然也是必不可少的了,還是老樣子結合代碼進行簡單介紹,首先看一下UICollect


UICollectionView在眾多控制項中也算是比較常用的了,像淘寶在瀏覽寶貝時採用的就是UICollectionView,對於UICollectionView->UICollectionViewFlowLayout當然也是必不可少的了,還是老樣子結合代碼進行簡單介紹,首先看一下UICollectionView實現結果:

實現這些功能很簡單,代碼量極少,線看一下代碼,然後進行深入瞭解:

//
//  ViewController.m
//  CX-UICollentionVIew基礎
//
//  Created by ma c on 16/3/19.
//  Copyright © 2016年 xubaoaichiyu. All rights reserved.
//

#import "ViewController.h"

static NSString * identifier = @"cxCellID";
@interface ViewController ()<UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView * collectionView;

@end

@implementation ViewController
#pragma mark - set_and_get
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        //自動網格佈局
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
        //網格佈局
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
        //註冊cell
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
        //設置數據源代理
        _collectionView.dataSource = self;
    }
    return _collectionView;
    
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.collectionView];
    
}
#pragma mark - deleDate
//有多少的分組
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
    
}
//每個分組裡有多少個item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 100;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //根據identifier從緩衝池裡去出cell
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor orangeColor];
    
    return cell;
    
}

@end

 

介紹過最最基本的知識點之後,按著步驟來,進行進一步介紹。

UICollectionViewFlowLayout的使用讓UICollectionView更加豐富多彩,下麵看一下使用UICollectionViewFlowLayout之後的效果吧->

代碼->

//
//  ViewController.m
//  CX-UICollentionVIew基礎
//
//  Created by ma c on 16/3/19.
//  Copyright © 2016年 xubaoaichiyu. All rights reserved.
//

#import "ViewController.h"

static NSString * identifier = @"cxCellID";
static CGFloat kMagin = 10.f;
static NSString * headIdentifier = @"cxHeadID";
@interface ViewController ()<UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView * collectionView;

@end

@implementation ViewController
#pragma mark - set_and_get
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        //自動網格佈局
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        CGFloat itemWidth = (self.view.frame.size.width - 4 * kMagin) / 3;
        
        //設置單元格大小
        flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth / 0.618);
        //最小行間距(預設為10)
        flowLayout.minimumLineSpacing = 10;
        //最小item間距(預設為10)
        flowLayout.minimumInteritemSpacing = 10;
        //設置senction的內邊距
        flowLayout.sectionInset = UIEdgeInsetsMake(kMagin, kMagin, kMagin, kMagin);
        //設置UICollectionView的滑動方向
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //sectionHeader的大小,如果是豎向滾動,只需設置Y值。如果是橫向,只需設置X值。
        flowLayout.headerReferenceSize = CGSizeMake(100,0);
        //網格佈局
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
        //註冊cell
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
        //註冊header
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headIdentifier];
        //設置數據源代理
        _collectionView.dataSource = self;
    }
    return _collectionView;
    
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.collectionView];
    
}
#pragma mark - deleDate
//有多少的分組
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
    
}
//每個分組裡有多少個item
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 100;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    //根據identifier從緩衝池裡去出cell
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor orangeColor];
    
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    //kind有兩種 一種時header 一種事footer
    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headIdentifier forIndexPath:indexPath];
        
        header.backgroundColor = [UIColor yellowColor];
        
        return header;
        
    }
    return nil;
    
}

@end

靈活的是使用 UICollectionView+UICollectionViewFlowLayout 會帶來更多的有趣的體驗,隨後我會介紹一些比較實用的實現。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 問題: 在做即時通訊時,需要提示用戶有幾條未讀的提醒,這個是(如果有新的提示消息立馬在瀏覽器無刷新提示)即時獲取的。但我們的做法是,當用戶點擊未讀信息進入到信息顯示頁面時重新獲取下未讀的提醒;但是在IE瀏覽器下,在新視窗打開以後沒有重新獲取請求,再次刷新頁面也沒有看到請求地址。但是如果將鏈接打開方式
  • "Whenever this property changes, apply that change slowly." The property transition: width 2s says “when the width changes, animate it over the course
  • 分類:C#、Android、VS2015; 創建日期:2016-03-19 一、簡介 Android系統定義了一系列獨立的圖形處理類,其中,2D圖形處理類分別位於以下命名空間: Android.Graphices Android.Graphics.Drawable.Shapes Android.Vi...
  • 1.安卓中文件的數據存儲實例(將文件保存到手機自帶存儲空間中): ①MainActivity.java public class MainActivity extends Activity implements OnClickListener{ private Button mButton; pri
  • 最好不要在UIViewController的loadView方法中改變狀態欄的可視性(比如狀態欄由顯示變為隱藏、或者由隱藏變為顯示),因為這樣的操作會導致重覆調用2次loadView和viewDidLoad方法。 雖然運行效果是對的,但是系統連續調用了2次loadView和viewDidLoad方法
  • 猴年支付寶可算是給大家一個很好的驚喜,刺激。大家都在為敬業福而四處奔波。可是到最後也沒有幾個得到敬業福德,就像我。不知道大家有沒有觀察,五福界面的滾動是一個很好的設計。在這裡,給大家帶來簡單的滾動實現,首先看一下實現效果。 通過觀察不難發現,有很多地方並不是那麼容易想出來的,對於篇隨筆,感興趣可以查
  • 1、界面 2、MainActivity代碼,用來響應button代碼 3、http請求同步代碼 4、把輸入流轉換為字元串 5、清單文件 <uses-permission android:name="android.permission.INTERNET"/>//許可權 最後:一般來說,子線程是無法改變
  • 因為最近在更新我的項目,就想著把自己在項目中用到的一些的簡單的與網路交互的方法總結一下,所以最近Android網路編程方面的博文會比較多一些,我儘量以最簡單的方法給大家分享,讓大家明白易懂。如果有什麼不對的地方,還請大家留言指出。 這次是利用PHP操作MySql,將Android上面輸入的數據插入到
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...