collectionView 和 tableView的嵌套使用

时间:2023-03-08 17:05:44

collectionView 和 tableView的嵌套使用

#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