美文网首页iOS踩过的坑系列
iOS踩过的坑之一个Controller中放两个collecti

iOS踩过的坑之一个Controller中放两个collecti

作者: Shaw1211 | 来源:发表于2019-04-24 14:29 被阅读0次

问题:两个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

相关文章

  • iOS踩过的坑之一个Controller中放两个collecti

    问题:两个CollectionView在使用瀑布流布局时,总是一个设置布局正常,另一个总显示不出来。原因:The ...

  • iOS项目中误删文件然后还原导致的问题

    本人初学的iOS,由于自己摸索,导出踩坑,特此随笔几下我踩过的坑,老鸟就不用点进来了。 一、加强对这个坑的认识,毕...

  • 交互设计师所要避免的几个坑

    前言 工作中难免会踩到几个坑,即使现在不踩以后还会踩,只有踩过才会深刻记住,踩过说明爱过!但是踩过的坑必须把坑填满...

  • iOS开发中遇到的坑

    从事iOS开发已有数年,一路走来踩过无数的坑,然而都踩过哪些坑,如今想来脑子里竟是一片空白,为什么呢?仔细想了想,...

  • 这些年掉过的坑-盲目跟风篇

    昨天写了我这些年踩过的两个坑,今天继续写踩过的第三个坑:头脑一团浆糊,盲目跟风。 李笑来在《财富自由之路》中说,很...

  • iOS framework踩过的坑

    1、 framework可以生成真机和虚拟机2个不同的framewrok,不能互通使用,所以必须要讲2个frame...

  • iOS之踩过的坑

    坑一 我们知道,在Xcode里,如果两个文件重名,编译的时候会报一个 2 duplicate symbols的错误...

  • iOS 开发踩过的坑

    1、navigationBar的透明问题 如果仅仅把navigationBar的alpha设为0的话,那就相当于把...

  • iOS10的适配

    每次出了新系统,必然要踩很多坑,这次来踩一踩iOS10的坑吧。 一、证书问题 直接选择Automatically ...

  • iOS集成OpenCV

    iOS项目集成OpenCV及踩过的坑 一、直接下载Framework集成 1.1、下载OpenCV的Framewo...

网友评论

    本文标题:iOS踩过的坑之一个Controller中放两个collecti

    本文链接:https://www.haomeiwen.com/subject/pvlggqtx.html