不完全使用(Masonry)给collectionview添加约束--通过contentoffset来给collectionview添加表头

31 篇文章 0 订阅
16 篇文章 0 订阅

//

//  ShopListMainViewController.m

//  WeiLi

//

//  Created by 张立志 on 16/10/13.

//  Copyright © 2016 张立志. All rights reserved.

//


#import "ShopListMainViewController.h"


#import "ConstantHeader.h"

#import <Masonry/Masonry.h>

#import "OJLWaterLayout.h"

#import "MBProgressHUD+QSJ.h"

#import "AlertUtil.h"

#import "MJRefresh.h"

#import <SDWebImage/UIImageView+WebCache.h>

#import <SDCycleScrollView/SDCycleScrollView.h>

#import "Model.h"

#import "DynamicExperienceCell.h"


@interface ShopListMainViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,OJLWaterLayoutDelegate>

//@property(nonatomic,strong) UIScrollView *mainScrollView;

@property(nonatomic,strong) UIView *contentView;

@property (nonatomic, strong) UICollectionView* collectionView;

@property (nonatomic, strong) NSMutableArray* modelArray;

@property (nonatomic, strong) OJLWaterLayout* layout;


@end


@implementation ShopListMainViewController


- (NSMutableArray *)modelArray

{

    if (!_modelArray) {

        _modelArray = [[Model models] mutableCopy];

    }

    return _modelArray;

}


- (void)viewDidLoad {

    [super viewDidLoad];

//    self.mainScrollView = [[UIScrollView alloc]init];

//    [self.view addSubview:self.mainScrollView];

//    self.mainScrollView.showsHorizontalScrollIndicator = NO;

//    self.mainScrollView.showsVerticalScrollIndicator = NO;

//    self.mainScrollView.bounces = NO;

//    self.mainScrollView.backgroundColor = NORMAL_BKG_GREY;

//    self.mainScrollView.scrollEnabled = YES;

//    [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {

//        make.edges.mas_equalTo(self.view);

//    }];

    

    //今日推荐的商品main

    OJLWaterLayout* layout = [[OJLWaterLayout alloc] init];

    self.layout = layout;

    layout.numberOfCol = 2;

    layout.rowPanding = 7;

    layout.colPanding = 7;

    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

    layout.delegate = self;

    [layout autuContentSize];

    //添加collection

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREEN_SIZE_HEIGHT, SCREEN_SIZE_WIDTH,SCREEN_SIZE_HEIGHT-49-64-40) collectionViewLayout:self.layout];

    self.collectionView.dataSource = self;

    self.collectionView.delegate = self;

    self.collectionView.backgroundColor = NORMAL_BKG_GREY;

    [self.view addSubview:self.collectionView];

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {

         make.edges.mas_equalTo(self.view);

    }];

    //注册cell

    [self.collectionView registerClass:[DynamicExperienceCell class] forCellWithReuseIdentifier:@"cell"];


    

    //contentView

    self.contentView = [[UIView alloc] init];

    [self.collectionView addSubview:self.contentView];

    //

    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(0);

        make.top.mas_equalTo(0);

        make.width.mas_equalTo(self.collectionView.mas_width);

        make.bottom.mas_equalTo(0);

    }];

    

    //轮播图

    UIView *lunBoView = [[UIView alloc]init];

    [self.contentView addSubview:lunBoView];

    lunBoView.backgroundColor = [UIColor yellowColor];

    [lunBoView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(self.contentView);

        make.left.mas_equalTo(self.contentView);

        make.width.mas_equalTo(SCREEN_SIZE_WIDTH);

        make.height.mas_equalTo(SCREEN_SIZE_WIDTH*2/5);

    }];

    

    //轮播图下面热销商品的标题

    UIView *reXiaoTitleView = [[UIView alloc]init];

    reXiaoTitleView.backgroundColor = NORMAL_BKG_GREY;

    [self.contentView addSubview:reXiaoTitleView];

    [reXiaoTitleView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(lunBoView.mas_bottom);

        make.left.mas_equalTo(self.contentView);

        make.width.mas_equalTo(SCREEN_SIZE_WIDTH);

        make.height.mas_equalTo(40);

    }];

//    UIView *lineOne = [[UIView alloc]init];

    

    UIView *reXiaoView = [[UIView alloc]init];

    [self.contentView addSubview:reXiaoView];

    reXiaoView.backgroundColor = [UIColor yellowColor];

    [reXiaoView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(reXiaoTitleView.mas_bottom);

        make.left.mas_equalTo(self.contentView);

        make.width.mas_equalTo(SCREEN_SIZE_WIDTH);

        make.height.mas_equalTo(SCREEN_SIZE_WIDTH*2/5);

    }];

    

    //下面活动专区商品的标题

    UIView *activityTitleView = [[UIView alloc]init];

    activityTitleView.backgroundColor = NORMAL_BKG_GREY;

    [self.contentView addSubview:activityTitleView];

    [activityTitleView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(reXiaoView.mas_bottom);

        make.left.mas_equalTo(self.contentView);

        make.width.mas_equalTo(SCREEN_SIZE_WIDTH);

        make.height.mas_equalTo(40);

    }];

    UIView *activityView = [[UIView alloc]init];

    [self.contentView addSubview:activityView];

    activityView.backgroundColor = [UIColor yellowColor];

    [activityView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(activityTitleView.mas_bottom);

        make.left.mas_equalTo(self.contentView);

        make.width.mas_equalTo(SCREEN_SIZE_WIDTH);

        make.height.mas_equalTo(SCREEN_SIZE_WIDTH*2/5);

    }];

    

    //今日推荐的商品的标题

    UIView *todayTitleView = [[UIView alloc]init];

    todayTitleView.backgroundColor = NORMAL_BKG_GREY;

    [self.contentView addSubview:todayTitleView];

    [todayTitleView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(activityView.mas_bottom);

        make.left.mas_equalTo(self.contentView);

        make.width.mas_equalTo(SCREEN_SIZE_WIDTH);

        make.height.mas_equalTo(40);

    }];

    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(0);

        make.top.mas_equalTo(0);

        make.width.mas_equalTo(self.collectionView.mas_width);

        make.bottom.mas_equalTo(todayTitleView.mas_bottom);

    }];

    //内容详细偏移

    self.collectionView.contentOffset = CGPointMake(0, (SCREEN_SIZE_WIDTH*2/5 +40)*3);

    


    

}


-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    self.tabBarController.tabBar.hidden = NO;

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark UICollectionViewDataSource, UICollectionViewDelegate

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.modelArray.count;

}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    

    

    

    DynamicExperienceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    

    //        cell.model = self.modelArray[indexPath.item];

    

    if (indexPath.row == 0) {

        cell.personNameLabel.text = @"asdfasdfasdfasfd";

        cell.agreeNumLabel.text = @"1111111111111";

    }else{

        cell.personNameLabel.text = @"张三";

        cell.agreeNumLabel.text = @"111111";

    }

    

    [cell setNeedsLayout];

    [cell layoutIfNeeded];

    return cell;

    

}


#pragma mark OJLWaterLayoutDelegate

-(CGFloat)OJLWaterLayout:(OJLWaterLayout *)OJLWaterLayout itemHeightForIndexPath:(NSIndexPath *)indexPath{

    

    Model* model = self.modelArray[indexPath.item];

    

    CGFloat width = ([UIScreen mainScreen].bounds.size.width - self.layout.sectionInset.left - self.layout.sectionInset.right - (self.layout.colPanding * (self.layout.numberOfCol - 1))) / self.layout.numberOfCol;

    

    //H = ( W / W )* H

    //图片高度

    CGFloat height = (width/[model.w floatValue])*[model.h floatValue];

    

    //这里面计算好各项高度值

    //title

    CGRect titleHeighR = [model.name boundingRectWithSize:CGSizeMake(width-2*TEXT_LINE_DISTANCE-3, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:LITTLE_FONT} context:nil];

    CGFloat titleHeignt = titleHeighR.size.height+6;

    //文字内容

    CGRect titleContentHeighR = [model.des boundingRectWithSize:CGSizeMake(width-2*TEXT_LINE_DISTANCE-3, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:LITTLE_FONT} context:nil];

    CGFloat titleContentHeignt = titleContentHeighR.size.height+6;

    

    //还要加上人头像

    height = height +titleHeignt+titleContentHeignt+(30+TEXT_LINE_DISTANCE*3);

    

    return height;

}

/*

#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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值