1. 程式人生 > >collectionView 和 tableView的嵌套使用

collectionView 和 tableView的嵌套使用

pro esp uiscreen eid per aso hit collect super

#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

技術分享

collectionView 和 tableView的嵌套使用