tabelVew嵌套CollectionView

来源:互联网 发布:mac口红cb96好看吗 编辑:程序博客网 时间:2024/06/07 03:17

重要的写在前面,写了一个tabelVew嵌套CollectionView的使用方法,相关的demo请点击demo链接
这里写图片描述

简单的说,就是每一个tabelVewCell里面都包含一个CollectionView.

其实思路很简单的:
1.创建一个tableVIew

加入数据源,加入tableView如果需要加入一个头视图,加入代理方法

-(UITableView *)tableView{if (!_tableView) {_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0 , SCREEN_WIDTH, SCREEN_HEIGHT)];_tableView.backgroundColor =[UIColor whiteColor];_tableView.dataSource =self;_tableView.delegate = self;[_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];_tableView.showsVerticalScrollIndicator = NO;[_tableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:@"HomeTableViewCell"];UIImageView *headerView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];headerView.image = [UIImage imageNamed:@"风景1.jpg"];_tableView.tableHeaderView = headerView;[_tableView reloadData];}return _tableView;}#pragma mark -  tableViewData 代理方法-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return self.dataArray.count;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{}

2.自定义tableVIewCell,每个tableVIewCell里面搞个collectionVIew

//在cell的初始化方法里面搞一个collectionVIew- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{}//在layoutSubviews计算每个tableVIewCell的collectionVIew的大小- (void)layoutSubviews{[super layoutSubviews];}#pragma mark -- Collection delegate//这是UICollectionView的点击方法,如果点击每个UICollectionViewCell需要在viewController里面响应的话。需要自定义一个代理方法- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {if([self.delegate respondsToSelector:@selector(CustomCollection:didSelectRowAtIndexPath:str:)]){[self.delegate CustomCollection:collectionView didSelectRowAtIndexPath:indexPath str:self.collectDataArray[indexPath.row]];}}

3.自定义一下collectionVIewCell

自定义collectionVIewCell
需要计算一下根据屏幕的宽度,没行可以显示几个

总结:有很多的项目中tabelVew嵌套CollectionView其实每个cell就是一行CollectionView,例如AppStore,这种只要设置CollectionView水平方向滑动就好了。
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
在iOS的开发过程中,如有问题可以与我联系!
邮箱:2877025939@qq.com