CollectionView总结 OC 2022

@interface <#class name#> () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView           * jk_collectionView;

@end
- (UICollectionView *)jk_collectionView {
    if (!_jk_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kScreen_Width - kAdaptedFloat(26)) / 4, kAdaptedFloat(65));
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _jk_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        _jk_collectionView.backgroundColor = [UIColor clearColor];
        _jk_collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _jk_collectionView.showsHorizontalScrollIndicator = NO;
        _jk_collectionView.showsVerticalScrollIndicator = NO;
        _jk_collectionView.scrollEnabled = NO;
        _jk_collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _jk_collectionView.dataSource = self;
        _jk_collectionView.delegate = self;
        [_jk_collectionView registerClass:[LabelImageCollectionCell class] forCellWithReuseIdentifier:[LabelImageCollectionCell jk_className]];
    }
    return _jk_collectionView;
}
#pragma mark ------- UICollectionViewDelegate -------

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

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[UICollectionViewCell jk_className] forIndexPath:indexPath];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 6) {
        return kAdaptedSize(157, 84);
    } else {
        return kAdaptedSize(73, 84);
    }
}

###UICollectionViewCell
实现UICollectionViewCell自适应文字宽度
https://blog.csdn.net/lvlemo/article/details/76607073

###CollectionViewDelegate

头部控件需要继承UICollectionReusableView,只会跑initWithFrame方法
[self.collectionView registerClass:[RDBookcaseReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:RDBookcaseReusableViewID];
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGSize size = CGSizeMake(kSCREEN_WIDTH, 200);
    return size;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *reusableview = nil;
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 头部视图
        RDBookcaseReusableView *tempReusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:RDBookcaseReusableViewID forIndexPath:indexPath];
        reusableview = tempReusableView;
    }
    return reusableview;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值