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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值