UICollectionView嵌套UICollectionVi
2017-05-12 本文已影响549人
MegahertzCHN
1.UICollectionView嵌套UICollectionView,为什么发生CollectionView横滑不了的现象?
#import "ZHRecommendViewController.h"
#import "ZHHomePageCollectionViewCell.h"
#import "ZHHomeOneLevelCC.h"
@interface ZHRecommendViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation ZHRecommendViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupCollectionView];
[self loadNewData];
[self setUseSuperview:NO];
[self followScrollView:self.collectionView];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self showNavBarAnimated:NO];
}
- (void)dealloc
{
[self stopFollowingScrollView];
}
- (void)setupCollectionView
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 100);
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.zh_width, self.view.zh_height - 64) collectionViewLayout:layout];
[self.view addSubview:self.collectionView];
self.collectionView.backgroundColor = [UIColor clearColor];
[self.collectionView registerClass:[ZHHomeOneLevelCC class] forCellWithReuseIdentifier:@"cellCollectionId"];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
UINib *nib = [UINib nibWithNibName:NSStringFromClass([ZHHomePageCollectionViewCell class]) bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"cellId"];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
}
- (void)loadNewData
{
self.lives = [NSMutableArray array];
for (NSInteger i = 0; i < 10; i++) {
ZHLiveModel *live = [[ZHLiveModel alloc] init];
live.animationFinished = YES;
[self.lives addObject:live];
[self.collectionView reloadData];
}
}
#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 3;
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
}
return self.lives.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
ZHHomeOneLevelCC *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellCollectionId" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
ZHHomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
// cell.botlabel.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];
cell.live = self.lives[indexPath.row];
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return CGSizeMake(self.view.zh_width, 130);
}
if (indexPath.row % 5 == 0) {
return CGSizeMake(self.view.frame.size.width, 200);
}
else if (indexPath.row % 5 == 1) {
return CGSizeMake(self.view.frame.size.width / 4, 130);
}
else if (indexPath.row % 5 == 2) {
return CGSizeMake(self.view.frame.size.width / 4 * 3 - 10, 130);
}
else if (indexPath.row % 5 == 3) {
return CGSizeMake(self.view.frame.size.width / 4 * 3 - 10, 130);
}
else {
return CGSizeMake(self.view.frame.size.width / 4, 130);
}
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
@end
#import "ZHHomePageCollectionViewCell.h"
@interface ZHHomePageCollectionViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *backImg;
@end
@implementation ZHHomePageCollectionViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return _backImg;
}
@end
#import "ZHHomeOneLevelCC.h"
@interface ZHHomeOneLevelCC ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation ZHHomeOneLevelCC
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//1.初始化layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//设置collectionView滚动方向
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
//该方法也可以设置itemSize
layout.itemSize =CGSizeMake(110, 150);
//2.初始化collectionView
self.collectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:layout];
[self.contentView addSubview:self.collectionView];
self.collectionView.backgroundColor = [UIColor clearColor];
//3.注册collectionViewCell
//注意,此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
//注册headerView 此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为reusableView
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
//4.设置代理
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
}
return self;
}
#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 9;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(90, 130);
}
@end
#######2.因为没有找到错误,我换了一种解决方案:UITableView嵌套UICollectionView
#import "ZHRecommendVC.h"
#import "ZHHome_OneLevelTC.h"
#import "ZHHome_TwoLevelTC.h"
#define search
@interface ZHRecommendVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) CGFloat preScrollViewContentOffsetY;
@end
@implementation ZHRecommendVC
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.zh_width, self.view.zh_height - 64) style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:tableView];
tableView.delegate = self;
tableView.dataSource = self;
[tableView registerClass:[ZHHome_OneLevelTC class] forCellReuseIdentifier:@"ZHHome_OneLevelTC"];
[tableView registerClass:[ZHHome_TwoLevelTC class] forCellReuseIdentifier:@"ZHHome_TwoLevelTC"];
_tableView = tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
ZHHome_OneLevelTC *cell = [tableView dequeueReusableCellWithIdentifier:@"ZHHome_OneLevelTC"];
return cell;
} else {
ZHHome_TwoLevelTC *cell = [tableView dequeueReusableCellWithIdentifier:@"ZHHome_TwoLevelTC"];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
return 100;
} else {
return (130 * 2 + 200 + 10 * 2 + 100) * 4;
}
return 44;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 搜索栏显示 100?
if (scrollView.contentOffset.y <= -45 && scrollView.contentOffset.y >= -80) {
_tableView.frame = CGRectMake(0, 100, self.view.zh_width, self.view.zh_height - 100);
[self.tableView setNeedsLayout];
}
// 滑动判断是否隐藏直播按钮和导航栏
if (scrollView.contentOffset.y > 100) {
self.preScrollViewContentOffsetY = scrollView.contentOffset.y;
self.navigationController.navigationBar.hidden = YES;
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
ZHLog(@"%f", scrollView.contentOffset.y);
if (scrollView.contentOffset.y <= -45 && scrollView.contentOffset.y >= -80) {
_tableView.frame = CGRectMake(0, 80, self.view.zh_width, self.view.zh_height - 80);
[self.tableView setNeedsLayout];
} else {
_tableView.frame = CGRectMake(0, 64, self.view.zh_width, self.view.zh_height - 64);
[self.tableView setNeedsLayout];
}
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (scrollView.contentOffset.y - self.preScrollViewContentOffsetY < 0) {
self.navigationController.navigationBar.hidden = NO;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#import "ZHHome_OneLevelTC.h"
#import "ZHHomeOneLevelCC.h"
@interface ZHHome_OneLevelTC ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation ZHHome_OneLevelTC
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(10, 10, 9, 10);
layout.itemSize = CGSizeMake(100, 100);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"itemId"];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
[self.contentView addSubview:self.collectionView];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.collectionView.frame = self.contentView.bounds;
}
#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemId" forIndexPath:indexPath];
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
#import "ZHHome_TwoLevelTC.h"
#import "ZHHomePageCollectionViewCell.h"
@interface ZHHome_TwoLevelTC ()<UICollectionViewDelegate, UICollectionViewDataSource>
@end
@implementation ZHHome_TwoLevelTC
{
UICollectionView *mainCollectionView;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (!(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) return nil;
//1.初始化layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//设置collectionView滚动方向
[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
//设置headerView的尺寸大小
layout.headerReferenceSize = CGSizeMake(self.contentView.frame.size.width, 100);
//该方法也可以设置itemSize
// layout.itemSize =CGSizeMake(110, 150);
//2.初始化collectionView
mainCollectionView = [[UICollectionView alloc] initWithFrame:self.contentView.bounds collectionViewLayout:layout];
[self.contentView addSubview:mainCollectionView];
mainCollectionView.backgroundColor = [UIColor blueColor];
mainCollectionView.autoresizingMask = UIViewAutoresizingNone;
//3.注册collectionViewCell
//注意,此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
UINib *nib = [UINib nibWithNibName:NSStringFromClass([ZHHomePageCollectionViewCell class]) bundle:nil];
[mainCollectionView registerNib:nib forCellWithReuseIdentifier:@"cellId"];
//注册headerView 此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为reusableView
[mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
//4.设置代理
mainCollectionView.delegate = self;
mainCollectionView.dataSource = self;
mainCollectionView.scrollEnabled = NO;
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
mainCollectionView.frame = self.contentView.bounds;
}
#pragma mark collectionView代理方法
//返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 4;
}
//每个section的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ZHHomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
// cell.botlabel.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];
cell.backgroundColor = [UIColor yellowColor];
return cell;
}
//设置每个item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row % 5 == 0) {
return CGSizeMake(self.contentView.frame.size.width, 200);
}
else if (indexPath.row % 5 == 1) {
return CGSizeMake(self.contentView.frame.size.width / 3, 130);
}
else if (indexPath.row % 5 == 2) {
return CGSizeMake(self.contentView.frame.size.width / 3 * 2 - 10, 130);
}
else if (indexPath.row % 5 == 3) {
return CGSizeMake(self.contentView.frame.size.width / 3 * 2 - 10, 130);
}
else {
return CGSizeMake(self.contentView.frame.size.width / 3, 130);
}
return CGSizeMake(0, 0);
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
//通过设置SupplementaryViewOfKind 来设置头部或者底部的view,其中 ReuseIdentifier 的值必须和 注册是填写的一致,本例都为 “reusableView”
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
headerView.backgroundColor =[UIColor grayColor];
UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
label.text = @"这是collectionView的头部";
label.font = [UIFont systemFontOfSize:20];
[headerView addSubview:label];
return headerView;
}
//点击item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
找到不能滑动界面的真凶:闲话不多说,上代码。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return _backImg;
}
原因:hitTestWithEvent方法将滑动拖拽的方法屏蔽,所以滑动的时候先响应了hitTestWithevent方法,放回了当前的图片,而不能出发任何别的方法。
本来原想着用这个测试一下hitTestWithEvent方法的作用,没想到自己给自己挖了这么大一个坑,然后埋了一堆一堆的雷,差点没把自己炸屎。证明了那句话:No Zuo No Die,(One More Try?),所以拿出来分享这个蠢死了的项目坑。
结论:
1.不是UICollectionView不能嵌套UICollectionView,是自己的代码可能有点问题。
2.UICollectionView不能滑动,也和滑动的方向无关。// 本来猜测是CollectionView有两个滑动方向会相互干扰。
3.后来替代的解决方案不是特别好,是UITableView嵌套UICollectionView,每一个UITableView的Cell嵌套了CollectionView,一直觉得怪怪的,本来就是每个Cell里面都是嵌套的CollectionView,所以cell的高度虽然可以算出来,但是总是感觉这样的构造很怪,超级怪......
今天解决别的问题的时候,无意间将代码注释了,这个问题就无意间解决了,后来找了一下本地修改的代码块,找到了问题所在。柳暗花明又一村。哈哈哈......