iOS開發-UI (三)Collection

来源:http://www.cnblogs.com/fcug/archive/2017/01/17/6294394.html
-Advertisement-
Play Games

知識點 1.UICollectionView的創建 2. UICollectionView的常用代理方法 3. UICollectionView相關XIB操作 UICollectionView的創建 1.UICollectionViewLayout 作用:控制collectionView佈局的一個抽 ...


知識點

1.UICollectionView的創建

2. UICollectionView的常用代理方法

3. UICollectionView相關XIB操作

================================

UICollectionView的創建

1.UICollectionViewLayout

作用:控制collectionView佈局的一個抽象類

 

註意:一般不直接使用這個類,因為這個類很多方法都沒有實現,只是規定了很多屬性和方法,自已定義佈局需要創建一個類繼承UICollectionViewLayout,然後設定自定義佈局

 

2.UICollectionViewFlowLayout

作用:系統寫好的一個瀑布流佈局

//使用系統寫好的一個瀑布流佈局

    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];

/*

     UICollectionViewScrollDirectionVertical,

     UICollectionViewScrollDirectionHorizontal

     */

    //設置滑動的方向

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    //設置item之間的最小間距(豎直滑動的時候,表示的橫向間距,水平滑動的時候,表示的是縱向間距)

    layout.minimumInteritemSpacing = 35;

    //設置item之間的最小間距(豎直滑動的時候,表示的縱向間距,水平滑動的時候,表示的是橫向間距)

    layout.minimumLineSpacing = 10;

 

3.- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;

作用:創建一個UICollectionView,必須提供一個UICollectionViewLayout

//實例化一個UICollectionView

    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

 

4.協議代理

1)UICollectionViewDataSource

2)UICollectionViewDelegateFlowLayout

================================

UICollectionView的常用代理方法

#pragma mark- UICollectionView的代理方法

1.- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

作用:返回組數

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    

    return 10;

}

2.- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;

作用:返回元素個數

//返回每組當中所有的元素個數

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return 10;

}

3.- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

作用:返回元素所使用的UICollectionViewCell

//返回每個元素所使用的UICollectionViewCell對象

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    //去復用隊列查找cell

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    //改變背景色

    cell.backgroundColor = [UIColor redColor];

    return cell;

}

4.- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

作用:返回元素的CGSize大小

//修改每一個item的寬度和高度

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    return CGSizeMake(100, 100);

}

5.- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

作用:返回元素之間允許的最小間距,如果是水平滑動,則代表垂直距離,如果豎直滑動,則代表橫向距離

//效果同layout.minimumInteritemSpeacing,二選一

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{   

    

}

 

6.- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;

作用:返回元素之間允許的最小間距,如果是水平滑動,則代表橫向距離,如果豎直滑動,則代表垂直距離

//效果同layout.minimumLineSpacing,二選一

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    

}

7.- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

作用:控制一組視圖和屏幕邊界的(上,左,下,右)距離

//返回每一組元素跟屏幕4個邊界的距離(上,左,下,右)

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    

    //創建一個UIEdgeInsets的結構體

    return UIEdgeInsetsMake(10, 10, 10, 10);

    

}

 

8.- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

作用:返回頭部視圖的CGSize,如果是水平滑動,則寬度有效,如果是豎直滑動,只有高度有效

//返回頭部視圖的寬和高

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    

    //註意:如果是豎直滑動,則只有高度有效,如果是水平滑動,則只有寬度有效

    return CGSizeMake(50, 50);

}

 

9.- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

作用:返回頭部視圖

//返回頭部和尾部視圖所使用的UICollectionReusableView對象

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    

    //由於頭部和尾部視圖的實例化都需要調用此方法,所以需要通過kind判斷生成的是頭部視圖還是尾部視圖

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        //表示需要創建頭部視圖

        //復用形式創建頭部視圖

        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];

        

        //改變背景色

        headerView.backgroundColor = [UIColor greenColor];

        

        return headerView;

        

    }

    

    return nil;

}

 

10.- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

作用:選中某個元素

//選中某一個item

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    

    NSLog(@"第%ld組第%ld個",indexPath.section,indexPath.item);

    

}

 


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

-Advertisement-
Play Games
更多相關文章
  • HTML DOM對象的屬性和方法介紹 DOM 是 Document Object Model(文檔對象模型)的縮寫。 DOM(文檔對象模型)是針對HTML和XML文檔的一個API(應用程式編程介面),它允許程式和腳本動態地訪問和更新文檔的內容,結構和樣式。 W3C DOM 標準被分為 3 個不同的部 ...
  • 簡單的說,versionCode是給機器看的,versionName是給人看的。更新的時候,機器根據versionCode判斷是升級還是降級,即使versionName(版本號)比以前的高,但是versionCode比以前的低,機器還是會判斷是降級。 ...
  • 先看需求效果圖: 幾個需求點: 1、顯示當月以及下個月的日曆 (可自行拓展更多月份) 2、首次點擊選擇“開始日期”,再次點擊選擇"結束日期" (1)、如果“開始日期” “結束日期” 相同 (2)、如果“開始日期” “結束日期” 不同,且“結束日期” 晚於 “開始日期” (3)、如果“結束日期” 早於 ...
  • 在報表填報成功後,發送消息至APP會提示數據已更新。再次期間用戶需要有查看該模板的許可權,如果沒有的話,則無法接受到提示信息。那麼在FineReport移動端中,如何手動推送APP消息呢? ...
  • Xcode8開放了新的一個Extension:Xcode Source Editor Extension,目的是讓開發者可以正規的自主為IDE編寫插件,雖然說系統現提供的功能還比較拮据,但是不妨礙我們瞭解和使用,本文主要介紹Xcode Source Editor Extension的功能,並演示一個 ...
  • 前言 Android開發中我們或多或少都會接觸到資料庫。Android中提供了一個占用記憶體極小的關係型資料庫 SQLite。雖然Android系統中提供了許多操作SQLite的API,但是我們還是需要手動去編寫SQL語句,這經常會出現一些莫名其妙的問題(
  • 蘋果的加急審核如何使用呢? 在iTunesconnect頁面,點擊右上角的“?”圖標,在彈出菜單中選擇“聯繫我們”, 聯繫我們 然後在Contact Us頁面,選擇“App Review” —> “App Store Review” —>” Request Expedited Review”, 加急 ...
  • 這篇知識很重要,最好全掌握: 知識點: 1.UIViewController基本認識 2.UIViewController之間的切換 3.UIViewController生命周期 4.多個Controller之間的數據如何交換 UIViewController 1.MVC設計模式 MVC設計模式:M ...
一周排行
    -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 ...