【最常用的几个文章:字体颜色、换行等等】

UICollectionView 的两种方法(在用Masonry

2021-03-01  本文已影响0人  阳光下的叶子呵
第一种例子
// 遵守代理
//  <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>

//
/// UICollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.scrollEnabled = NO;
    [collectionView registerClass:[VideoChildCell class] forCellWithReuseIdentifier:NSStringFromClass([VideoChildCell class])];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:collectionView];
    [collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.mas_equalTo(self.titleTipsL.mas_bottom).offset([UIScreen mainScreen].bounds.size.width/375*15);
        make.bottom.mas_equalTo(self.lookAllVIPVideoBut.mas_top).offset(-[UIScreen mainScreen].bounds.size.width/375*10);
        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width/375*345);
    }];
    self.collectionView = collectionView;
    self.collectionView.backgroundColor = [UIColor clearColor];

// 代理等方法:
#pragma mark -- <UICollectionViewDelegate, UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.vipVideoArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    // item 大小
    return CGSizeMake([UIScreen mainScreen].bounds.size.width/375*166, [UIScreen mainScreen].bounds.size.width/375*166);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    // 行间距(上下)
    return [UIScreen mainScreen].bounds.size.width/375*1;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    // 列间距(左右)
    return [UIScreen mainScreen].bounds.size.width/375*2;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake([UIScreen mainScreen].bounds.size.width/375*6, 0, [UIScreen mainScreen].bounds.size.width/375*4, 0);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    WY_VIPVideoChildCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([WY_VIPVideoChildCell class]) forIndexPath:indexPath];
    
    cell.dataDic = self.vipVideoArray[indexPath.row];
    __weak typeof(self) weakSelf = self;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WY_VIPVideoChildCell *cell = (WY_VIPVideoChildCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    if (self.vipVideoDetailBlock) {
        self.vipVideoDetailBlock([NSString stringWithFormat:@"%@", self.vipVideoArray[indexPath.row][@"video_group_uuid"]]);
    }
}

第二种例子 设置layout布局:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.sectionHeadersPinToVisibleBounds = NO;
    layout.sectionFootersPinToVisibleBounds = NO;
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    
    [collectionView registerClass:[VideoFilterCollectionViewCell class] forCellWithReuseIdentifier:@"VideoFilterCollectionViewCell"];
    [collectionView registerClass:[VideoFilterHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"VideoFilterHeader"];
    collectionView.scrollEnabled = YES; // 禁止滚动
    
    [self.childView addSubview:collectionView];
    self.collectionView = collectionView;
    [collectionView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(separatorView.mas_bottom).offset(ScreenWidth/375*14);
        make.left.mas_equalTo(self.childView.mas_left).offset(ScreenWidth/375*15.5);
        make.right.mas_equalTo(self.childView.mas_right).offset(-ScreenWidth/375*13.5);
        make.bottom.mas_equalTo(self.childView.mas_bottom).offset(-kTabBarHeight);
    }];

UICollectionViewFlowLayout:直接设置各个属性:

UICollectionViewFlowLayout *numberLayout = [[UICollectionViewFlowLayout alloc] init];
    numberLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    numberLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/375*31, [UIScreen mainScreen].bounds.size.width/375*33);
    numberLayout.minimumLineSpacing = 0;
    numberLayout.minimumInteritemSpacing = 0;
    numberLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    numberLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _numberCollectionView= [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:numberLayout];
    _numberCollectionView.backgroundColor = RGBA(99, 102, 106, 1.0);
    _numberCollectionView.showsHorizontalScrollIndicator = NO;
    _numberCollectionView.showsVerticalScrollIndicator   = NO;
    _numberCollectionView.delegate                       = self;
    _numberCollectionView.dataSource                     = self;
    [self.contentView addSubview:self.numberCollectionView];
    [self.numberCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.numberHeaderLab);
        make.left.mas_equalTo(self.numberHeaderLab.mas_right);
        make.right.mas_equalTo(self.numberFooterLab.mas_left);
        make.height.mas_equalTo((is_HorizontalScreen_Width-is_Width_Left-10)/375*(15));
    }];
    //[self.numberCollectionView registerClass:[GW_FullScreenShowScoringHeaderCell class] forCellWithReuseIdentifier:@"GW_FullScreenShowScoringHeaderCell"];
    [self.collectionView registerClass:[GWHomeSelectCityPopChildCell class] forCellWithReuseIdentifier:NSStringFromClass([GWHomeSelectCityPopChildCell class])];

#pragma mark -- -- collection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.holesArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // GW_FullScreenShowScoringHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GW_FullScreenShowScoringHeaderCell" forIndexPath:indexPath];
    GWHomeSelectCityPopChildCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([GWHomeSelectCityPopChildCell class]) forIndexPath:indexPath];

    return cell;
}
上一篇 下一篇

猜你喜欢

热点阅读