iOS踩过的坑之一个Controller中放两个collecti
2019-04-24 本文已影响0人
Shaw1211
问题:两个CollectionView在使用瀑布流布局时,总是一个设置布局正常,另一个总显示不出来。
原因:The problem was that I was using the same layout object for each collection. In retrospect that makes sense, but you have to make sure you create different layouts for each collectionView.
解决方案
一个collectionView对象需要一个layout对象
#import "ViewController.h"
#import "NDUserCell.h"
#define MAX_JOINED 30
@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *topUserCollectionView;
@property (nonatomic, strong) UICollectionView *bottomUserCollectionView;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) NSMutableArray *allVideoViews;
@property (nonatomic, strong) NSMutableArray *allTopVideoViews;
@property (nonatomic, strong) NSMutableArray *allBottomVideoViews;
@end
static NSString * const topReuseIdentifier = @"top";
static NSString * const bottomReuseIdentifier = @"bottom";
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initUI];
[self registerCell];
[self configVideoViews];
}
- (void)reload {
[self.topUserCollectionView reloadData];
[self.bottomUserCollectionView reloadData];
}
- (void)registerCell {
[self.topUserCollectionView registerClass:[NDUserCell class] forCellWithReuseIdentifier:topReuseIdentifier];
[self.bottomUserCollectionView registerClass:[NDUserCell class] forCellWithReuseIdentifier:bottomReuseIdentifier];
}
- (void)initUI {
self.backgroundView = [UIView new];
self.backgroundView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.backgroundView];
[self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
self.topView = [UIView new];
[self.backgroundView addSubview:self.topView];
[self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
make.leading.width.equalTo(self.backgroundView);
//加上顶部导航栏和状态栏的高度
make.top.mas_equalTo(TOP_LAYOUT_HEIGHT);
//100为小屏变长,上下各10的边距
make.height.mas_equalTo(120);
}];
self.bottomView = [UIView new];
[self.backgroundView addSubview:self.bottomView];
[self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.leading.width.equalTo(self.backgroundView);
//100为小屏边长,上下各10的边距
make.height.mas_equalTo(120);
}];
UICollectionViewFlowLayout *topFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[topFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[topFlowLayout setItemSize:CGSizeMake(100.f, 100.f)];//设置cell的尺寸
topFlowLayout.sectionInset = UIEdgeInsetsMake(10.f, 15.f, 10.f, 15.f);//设置单元格在collectionView中的内边距,距上下各10,左侧15,相隔15
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////顶部CollectionView/////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
_topUserCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5.f, 0, SCREEN_WIDTH, 120.f) collectionViewLayout:topFlowLayout];//设置collectionView距顶部视图左边距5,宽度同屏幕,高度120
_topUserCollectionView.tag = 101;
_topUserCollectionView.backgroundColor = [UIColor yellowColor];
// [_topUserCollectionView setCollectionViewLayout:flowLayout];
[_topView addSubview:_topUserCollectionView];
//设置collectionView距顶部视图左边距5,宽度同屏幕,高度120
[_topUserCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.trailing.equalTo(self.topView);
make.leading.equalTo(self.topView).offset(5);
make.height.mas_equalTo(120.f);
}];
_topUserCollectionView.delegate = self;
_topUserCollectionView.dataSource = self;
//有导航栏时自动滚动调整,默认为YES
self.automaticallyAdjustsScrollViewInsets = NO;
_topUserCollectionView.showsHorizontalScrollIndicator = NO;
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////底部CollectionView/////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
UICollectionViewFlowLayout *bottomFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[bottomFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[bottomFlowLayout setItemSize:CGSizeMake(100.f, 100.f)];//设置cell的尺寸
bottomFlowLayout.sectionInset = UIEdgeInsetsMake(10.f, 15.f, 10.f, 15.f);//设置单元格在collectionView中的内边距,距上下各10,左侧15,相隔15
_bottomUserCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(5.f, SCREEN_HEIGHT - TOP_LAYOUT_HEIGHT - 120.f, SCREEN_WIDTH, 120.f) collectionViewLayout:bottomFlowLayout];//设置collectionView距顶部视图左边距5,宽度同屏幕,高度120
_bottomUserCollectionView.tag = 102;
_bottomUserCollectionView.backgroundColor = [UIColor yellowColor];
// [_topUserCollectionView setCollectionViewLayout:flowLayout];
[_bottomView addSubview:_bottomUserCollectionView];
//设置collectionView距顶部视图左边距5,宽度同屏幕,高度120
[_bottomUserCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.trailing.equalTo(self.bottomView);
make.leading.equalTo(self.bottomView).offset(5);
make.height.mas_equalTo(120.f);
}];
_bottomUserCollectionView.delegate = self;
_bottomUserCollectionView.dataSource = self;
_bottomUserCollectionView.showsHorizontalScrollIndicator = NO;
}
- (void)configVideoViews {
self.allTopVideoViews = [NSMutableArray array];
self.allBottomVideoViews = [NSMutableArray array];
for (int i = 0; i < self.allVideoViews.count; i++) {
if (i < 25) {
[self.allTopVideoViews addObject:self.allVideoViews[i]];
} else {
[self.allBottomVideoViews addObject:self.allVideoViews[i]];
}
}
[self.topUserCollectionView reloadData];
[self.bottomUserCollectionView reloadData];
}
- (NSMutableArray *)allVideoViews {
if (!_allVideoViews) {
_allVideoViews = [NSMutableArray array];
for (int i = 0; i < MAX_JOINED; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.f green:arc4random()%256/255.f blue:arc4random()%256/255.f alpha:.5f];
[_allVideoViews addObject:view];
}
}
return _allVideoViews;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (collectionView.tag == 101) {
LogRed(@"self.allTopVideoViews.count = %lu", self.allTopVideoViews.count);
return self.allTopVideoViews.count;
} else if (collectionView.tag == 102) {
LogRed(@"self.allBottomVideoViews.count = %lu", self.allBottomVideoViews.count);
return self.allBottomVideoViews.count;
}
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (collectionView.tag == 101) {
NDUserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:topReuseIdentifier forIndexPath:indexPath];
cell.videoView = self.allTopVideoViews[indexPath.row];
return cell;
} else if (collectionView.tag == 102) {
NDUserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:bottomReuseIdentifier forIndexPath:indexPath];
cell.videoView = self.allBottomVideoViews[indexPath.row];
return cell;
}
return nil;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
LogBlue(@"点击了一个item = %lu", indexPath.row);
}
@end