美文网首页
UICollectionViewCell 的正确写法

UICollectionViewCell 的正确写法

作者: 全世界妳最美 | 来源:发表于2018-08-13 11:11 被阅读301次

    1.原生的

    
    #import "WJBroadcastController.h"
    
    static NSString * identifier = @"cxCellID";
    static NSString * headIdentifier = @"headIdentifier";
    
    @interface WJBroadcastController ()<UICollectionViewDelegate,UICollectionViewDataSource>
    
    @property (nonatomic,strong)UICollectionViewFlowLayout  *collectionFlowLayout;
    @property (nonatomic,strong)UICollectionView *collectionView;
    
    @end
    
    @implementation WJBroadcastController
    - (UICollectionView *)collectionView{
        if (!_collectionView) {
            _collectionView = [[UICollectionView alloc] initWithFrame:CGRectNull collectionViewLayout:self.collectionFlowLayout];
            _collectionView.backgroundColor = QkColor;
            _collectionView.allowsMultipleSelection = NO;//是否支持多选
            _collectionView.delegate = self;
            _collectionView.dataSource = self;
        }
        return _collectionView;
        
    }
    // 可以自定义一些
    -(UICollectionViewFlowLayout *)collectionFlowLayout{
        if (!_collectionFlowLayout) {
            _collectionFlowLayout = [[UICollectionViewFlowLayout alloc] init];
            _collectionFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
            _collectionFlowLayout.itemSize = CGSizeMake(100, 100);//每个item的大小
            //_collectionFlowLayout.minimumLineSpacing = 50;//行间距
            //_collectionFlowLayout.minimumInteritemSpacing = 10;//item之间的距离
            _collectionFlowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);//每组的位置间距
    //        _collectionFlowLayout.headerReferenceSize = CGSizeMake(ScreenW, 80);
    //        _collectionFlowLayout.footerReferenceSize = CGSizeMake(ScreenW, 40);
        }
        return _collectionFlowLayout;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.view addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(ScreenW, 500));
            make.centerX.equalTo(self.view);
            make.top.mas_equalTo(self.view);
        }];
        //注册collection 的cell
         [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
        //注册collection 的header;
         [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headIdentifier];
    }
    
    #pragma mark - cell代理
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
        
        return 2;
    }
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return 9;
    }
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
        UICollectionViewCell * cell  = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
       
        cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
        for (UIView *view in cell.contentView.subviews) {
            if (view) {
              [view removeFromSuperview];
            }
        }
        
        
        return cell;
    }
    //选择了某个collection
    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
        UICollectionViewCell *cell =  [collectionView cellForItemAtIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor redColor]];
    }
    //取消某个collection
    - (void)collectionView:(UICollectionView*)collectionView didDeselectItemAtIndexPath:(NSIndexPath*)indexPath
    
    {
        UICollectionViewCell*cell = [collectionView cellForItemAtIndexPath:indexPath];
        [cell setBackgroundColor:[UIColor greenColor]];
        
    }
    
    #pragma mark - 头视图的代理
    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headIdentifier forIndexPath:indexPath];
        header.backgroundColor = [UIColor yellowColor];
    #warning message - 不同组的头视图(不一样)。
        // 解决头视图不一样的问题 添加多个头视图,以及设置他们的大小
        NSInteger index = indexPath.section;
        if (index == 0) {
          
        }
        if (index == 1) {
            
        }
        return header;
    }
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
        //return CGSizeMake(头视图的宽, 头视图的高);
        return CGSizeMake(ScreenW, 50);
    }
    
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
        
        //return CGSizeMake(尾视图的宽, 尾视图的高);
        return CGSizeMake(ScreenW, 50);
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    /*
    #pragma mark - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */
    
    @end
    
    

    2.简单的分装一下。cell 以及头视图或者尾部试图

    2.1 -----------------------cell
    
    #define cellWidth self.contentView.bounds.size.width -10
    #import "FirstMusicCollectionCell.h"
    
    
    @interface FirstMusicCollectionCell ()
    
    @property (nonatomic,strong)UIButton *butimage;
    @property (nonatomic,strong)UILabel *lab1;
    @property (nonatomic,strong)UILabel *lab2;
    
    @end
    
    @implementation FirstMusicCollectionCell
    
    static FirstMusicCollectionCell *cell;
    
    + (instancetype)collection:(UICollectionView *)collectionView indexpath:(NSIndexPath *)indexpath{
        NSString *cellid = @"FirstMusicCollectionCell";
        [collectionView registerClass:[FirstMusicCollectionCell class] forCellWithReuseIdentifier:cellid];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellid forIndexPath:indexpath];
        return cell;
    }
    - (instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            [self hg_setAllCornerWithCornerRadius:4];
            [self creatUI];
        }
        return self;
    }
    - (void)creatUI{
        UIButton *but = [[UIButton alloc] init];
        [self.contentView addSubview:but];
        but.userInteractionEnabled = NO;
        but.backgroundColor = [UIColor redColor];
        [but hg_setAllCornerWithCornerRadius:4];
        [but mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(cellWidth, cellWidth));
            make.centerX.equalTo(self.contentView);
            make.top.mas_equalTo(self.contentView).offset(5);
        }];
        UILabel *lab1 = [[UILabel alloc] init];
        [self.contentView addSubview:lab1];
        lab1.font = font(12);
        lab1.textAlignment = NSTextAlignmentLeft;
        [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.size.mas_equalTo(CGSizeMake(cellWidth, 15));
            make.top.mas_equalTo(but.mas_bottom);
        }];
        UILabel *lab2 = [[UILabel alloc] init];
        lab2.textColor = [UIColor colorTextGray];
        [self.contentView addSubview:lab2];
        lab2.font = font(12);
        lab2.textAlignment = NSTextAlignmentLeft;
        [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contentView);
            make.size.mas_equalTo(CGSizeMake(cellWidth, 15));
            make.top.mas_equalTo(lab1.mas_bottom);
        }];
        _butimage = but;
        _lab1 = lab1;
        _lab2 = lab2;
    }
    - (void)setModel:(FirstMusicModel *)model
    {
        _model = model;
    //    [_butimage setImage:self.model.image];
        _lab1.text = self.model.text1;
        _lab2.text = self.model.text2;
    }
    - (void)setModel2:(FirstMusicListModel *)model2
    {
        _model2 = model2;
    //    [_butimage setImage:self.model2.image];
        _lab1.text = self.model2.text1;
        _lab2.text = self.model2.text2;
    }
    #pragma mark- 解决重用问题
    //预防重用-- 目前没遇到问题
    - (void)prepareForReuse {
        //NSLog(@"MyCollection9999999%s --- %@", __func__, self);
    }
    
    @end
    
    

    2.2 头部试图 *********** 不要一个里面写好几个,分开写

    //
    //  FirstMusciHeader2.m
    //  ParallelWorld
    //
    //  Created by 齐凯 on 2018/10/12.
    //  Copyright © 2018年 Mac. All rights reserved.
    //
    
    #import "FirstMusciHeader2.h"
    @interface FirstMusciHeader2()
    @property (nonatomic,strong)UIView *view1;
    @end
    
    @implementation FirstMusciHeader2
    static FirstMusciHeader2 *header;
    
    + (instancetype)collectionResuableView:(UICollectionView *)collectionView indexpath:(NSIndexPath *)indexPath{
        NSString *cellID = @"FirstMusciHeader2";
        //注册collection 的header;
        [collectionView registerClass:[FirstMusciHeader2 class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:cellID];
        header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:cellID forIndexPath:indexPath];
        return header;
    }
    - (instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            [self creatUI];
            self.backgroundColor = [UIColor blackColor];
        }
        return self;
    }
    - (void)creatUI{
        
        [self view1];
    }
    
    - (UIView *)view1{
        if (!_view1) {
            _view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 40, ScreenW, 40)];
            _view1.backgroundColor = [UIColor whiteColor];
            [self addSubview:self.view1];
            UILabel *lab = [[UILabel alloc] init];
            lab.text = @"热门单曲";
            lab.textAlignment = NSTextAlignmentCenter;
            lab.font = [UIFont systemFontOfSize:17];
            [_view1  addSubview:lab];
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(200, 40));
                make.center.equalTo(self.view1);
            }];
            UIButton *but = [[UIButton alloc] init];
            [but addTarget:self action:@selector(butNextBtn) forControlEvents:(UIControlEventTouchUpInside)];
            [_view1 addSubview:but];
            [but setImage:@"更多_28"];
            [but mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(30, 30));
                make.centerY.equalTo(self.view1);
                make.right.mas_equalTo(self.view1);
            }];
        }
        return _view1;
    }
    #pragma mark - 点击事件
    - (void)butNextBtn{
        //更多
        
    }
    @end
    
    
    

    相关文章

      网友评论

          本文标题:UICollectionViewCell 的正确写法

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