collectionView 和 tableView的嵌套使用

来源:http://www.cnblogs.com/longyongping/archive/2017/05/31/6923122.html
-Advertisement-
Play Games

/Users/longyongping/Desktop/WX20170531-114430.png ...


 

#import "ViewController.h"

#define HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH [UIScreen mainScreen].bounds.size.width

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIView* backvView;

@property(nonatomic,strong)UIScrollView* secondScrollView;

@property(nonatomic,strong)UICollectionView* collectionView;

@property(nonatomic,strong)UITableView * tableView;

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    //添加ui

    [self AddUI];

    //添加集合視圖

    [self collectionViewMethods];

    //添加單元格

    [self tableViewMethods];

}

 

 

-(void)AddUI

{

    _backvView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-40)];

    _backvView.backgroundColor=[UIColor cyanColor];

    [self.view addSubview:_backvView];

    

    UIImageView* fristImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 520.0/2208.0*HEIGHT)];

    fristImageView.backgroundColor = [UIColor yellowColor];

    [_backvView addSubview:fristImageView];

    

    

    _secondScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 520.0/2208.0*HEIGHT, WIDTH, 640.0/2208.0*HEIGHT)];

    _secondScrollView.backgroundColor=[UIColor blueColor];

    [_backvView addSubview:_secondScrollView];

    

    UIView * thirdView=[[UIView alloc]initWithFrame:CGRectMake(0, 1160.0/2208.0*HEIGHT, WIDTH,30.0/2208.0*HEIGHT)];

    thirdView.backgroundColor=[UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];

    [_backvView addSubview:thirdView];

    

    UIView* fourthView=[[UIView alloc]initWithFrame:CGRectMake(0, 1190.0/2208.0*HEIGHT, WIDTH, 830.0/2208.0*HEIGHT)];

    fourthView.backgroundColor=[UIColor greenColor];

    [_backvView addSubview:fourthView];

    

    UIView* fifthView=[[UIView alloc]initWithFrame:CGRectMake(0, 2010.0/2208.0*HEIGHT, WIDTH, 45.0/2208.0*HEIGHT)];

    fifthView.backgroundColor=[UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1];

    [_backvView addSubview:fifthView];

}

 

-(void)tableViewMethods

{

    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];

    

    _tableView.delegate=self;

    

    _tableView.dataSource=self;

    

    [self.view addSubview:_tableView];

 

    _tableView.tableHeaderView=_backvView;

}

 

 

-(void)collectionViewMethods

{

    //初始化集合視圖對象

    UICollectionViewFlowLayout* layout=[[UICollectionViewFlowLayout alloc]init];

    //設置集合視圖滾動方向

    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

    //設置item的大小

    [layout setItemSize:CGSizeMake(150.0/1242.0*WIDTH, 150.0/2208.0*HEIGHT)];

    //設置集合視圖的範圍   距離邊距   依次是上左下右

    [layout setSectionInset:UIEdgeInsetsMake(70.0/2208.0*HEIGHT, 70.0/1242.0*WIDTH, 100.0/2208.0*HEIGHT, 70.0/1242.0*WIDTH)];

    

    //設置最小行間距

    layout.minimumLineSpacing = 90.0/1242.0*WIDTH;

    //最小列間距

    layout.minimumInteritemSpacing = 60.0/2208.0*HEIGHT;

    

    [self setCollectionView:[[UICollectionView alloc]initWithFrame: CGRectMake(0, 0, WIDTH, 640.0/2208.0*HEIGHT)collectionViewLayout:layout]];

    

    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    

    _collectionView.delegate = self;

    

    _collectionView.dataSource = self;

    

    _collectionView.backgroundColor = [UIColor whiteColor];

    

    [_secondScrollView addSubview:_collectionView];

    

}

 

 

#pragma mark -UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 10;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

    //創建一個靜態的字元串  當作單元格的標記

    static NSString* cellId=@"cell";

    //先表格視圖的單元格池        中獲取有該標記的單元格  獲取到的單元格上暫時沒有被使用的

    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:cellId];

 

    if (cell==nil)

    {

        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];

    }

    return cell;

}

 

#pragma mark -UICollectionViewDelegate

 

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

    

    return 20;

}

 

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

{

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

    

    [cell setBackgroundColor:[UIColor colorWithRed:arc4random () % 255 /255.0 green:arc4random () % 255 /255.0 blue:arc4random () % 255 /255.0 alpha:1]];

    

    return cell;

}

@end

 


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

-Advertisement-
Play Games
更多相關文章
  • 本篇是在VPS上搭建Hexo靜態博客的第一篇博文,寫本篇的目的一是紀念一下,二是作為一個部署文檔保留。 "博客地址" 相關描述 VPS環境是在 "搬瓦工" 上安裝的centos6(x86),1核,512MB,10GB,1000GB/月的配置($19.99/年,可用支付寶支付),主要的功能是搭個梯子, ...
  • [1]路徑組成 [2]分隔符 [3]規範化 [4]絕對和相對 [5] [6] [7] ...
  • 用react一年多了.一直是在別人的影子下寫的代碼,他們也確實都是大神級的人物,不過,小菜鳥也有小菜鳥的思想~這不,今天就在重溫一遍react!記一些零碎的知識點~不知道對你們有沒有用,不過,對於我,絕對有用!所有的這些均參考於http://www.ruanyifeng.com/blog/2015/ ...
  • http://blog.csdn.net/chaozhung_no_l/article/details/49929177 轉自這位大神的博客,感謝這位大神,幫了大忙,謝謝!! ...
  • 1. iOS兩種自適應佈局方式:(修正說明:) -AutoLayout(自動佈局) + SizeClasses(尺寸類別) -Autoresizing (自動調整尺寸/彈簧式調整尺寸) 前者 AutoLayout 是從iOS6出現,通過創建視圖約束實現自適應,SizeClasses是iOS8 開始出 ...
  • 一、基本介紹 WebView是一個顯示頁面的組件。 二、基本使用 訪問網頁需要網路,所以在AndroidManifest.xml文件中添加網路許可權。 可以使用Intent跳轉網頁,使用如下: 使用Intent跳轉網頁,會調用本地的瀏覽器來查看網頁信息。 在onCreate()方法中將contentV ...
  • FragmentActivity + Fragment(通過hide和show來顯示fragment) + Fragment(通過viewpager來顯示fragment) 在Activity中 在佈局中 然後在fragment中會添加一個viewpager來再添加幾個fragment ...
  • 本節引言: 本節帶來的是Android基本UI控制項中的第十個:ScrollView(滾動條),或者我們應該叫他 豎直滾動條,對應的另外一個水平方向上的滾動條:HorizontalScrollView,先來一發官方文檔 的鏈接:ScrollView,我們可以看到類的結構如下: 嘿嘿,原來是一個Fram ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...