小工具知识网

当前位置: 首页 > 行业知识 > CollectionView总结 OC 2022

行业知识

CollectionView总结 OC 2022

2026-11-30 14:23:43 来源:互联网转载
@interface  () @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

()

实article/index.php/tags-41973.html" >���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;}

上一篇:电脑小桌面下载指南,轻松掌握,一键直达!

下一篇:电脑视频做壁纸教程简单