美文网首页
UICollectionView

UICollectionView

作者: 你的小福蝶 | 来源:发表于2018-04-11 20:04 被阅读24次

UITableViewCell中嵌套CollectionView

.h文件

#import <UIKit/UIKit.h>

typedef void(^ManagerBusinessBlock)(NSInteger index);

@interface WorkTabManagerBusinessCell : UITableViewCell

@property(nonatomic,strong) NSArray *dataArr;

@property (nonatomic , assign) ManagerBusinessBlock myBlock;

+ (WorkTabManagerBusinessCell *)cellWithTableView:(UITableView *)tableView;

@end

.m文件

#import "WorkTabManagerBusinessCell.h"
#import "YJHeader.h"

@interface WorkTabManagerBusinessCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic , strong) UICollectionView *collectionView;

@end

@implementation WorkTabManagerBusinessCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = hexStringToColor(COLOR_BG);
        
        [self setUpTheUIMethod];
        
    }
    return self;
}

+ (WorkTabManagerBusinessCell *)cellWithTableView:(UITableView *)tableView{
    static NSString *identifier = @"worktabManabusiceiden";
    WorkTabManagerBusinessCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[WorkTabManagerBusinessCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setUpTheUIMethod{
    
    [self.contentView addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"worktabmanagbusiceident"];
}

- (void)setDataArr:(NSArray *)dataArr{
    
    _dataArr = dataArr;
    [self.collectionView reloadData];
}

#pragma mark  -   UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(0, 0);
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"worktabmanagbusiceident";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    return cell;
}


#pragma mark -   UICollectionViewDelegateFlowLayout
//定义每个Item 的大小    --  可在layout直接设置
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake( W,H);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if (self.myBlock) {
        self.myBlock(indexPath.item);
    }
}

#pragma mark   -   LazyLoadMethod

- (UICollectionView *)collectionView{
    if (!_collectionView) {

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 10;//每行之间竖直之间的最小间距 (可以大于)
        flowLayout.minimumInteritemSpacing = 0;//同行最小间距(可以)
        flowLayout.itemSize = CGSizeMake(itemWidth, itemHeight);//建议使用layout代理设置
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//滚动方向
        flowLayout. headerReferenceSize = ;
        flowLayout. footerReferenceSize = ;
        flowLayout. footerReferenceSize = UIEdgeInsetsMake(10, 10, 10, 10);//item组的四周间隔
        flowLayout. estimatedItemSize = ;//预估cell的尺寸(ios8)自适应

        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-TabBarHeight-NavHeight-(5*2+78*2+215)*layoutBy6()) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = hexStringToColor(@"ffffff");
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = YES;
        //当使用自定义cell时需注册否则无法重用
        [_collectionView registerClass:[自定义CollectionCell class] forCellWithReuseIdentifier:@"zidingyiidenti"];
        //使用Layout的Header或Footer时也需注册
    }
    return _collectionView;
}

相关文章

网友评论

      本文标题:UICollectionView

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