美文网首页
CollectionView总结 OC 2020

CollectionView总结 OC 2020

作者: Look2021 | 来源:发表于2020-06-17 18:07 被阅读0次
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = kAdaptedSize(90, 90);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.contentInset = UIEdgeInsetsMake(0, kAdaptedFloat(33), 0, 0);
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.scrollEnabled = NO;
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:[UICollectionViewCell jk_className]];
    }
    return _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;
}

相关文章

网友评论

      本文标题:CollectionView总结 OC 2020

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