tabelVew嵌套CollectionView

重要的写在前面,写了一个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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,792评论 1 9
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 2,920评论 2 10
  • { 24、Sqlite数据库 1、存储大数据量,增删改查,常见管理系统:Oracle、MSSQLServer、DB...
    CYC666阅读 965评论 0 1
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,094评论 3 38
  • 我本红尘客 偶做网前人 笑谈天与地 妄议古和今 闲逗娇羞女 也有知心人 俗世不逐流 赤子心自真
    狼豪难蘸墨阅读 265评论 0 0