美文网首页iOS进阶iOS开发笔录iOS学习
iOS 在使用Masonry的情况下实现tableViewCel

iOS 在使用Masonry的情况下实现tableViewCel

作者: DeepChafferer | 来源:发表于2017-05-26 09:37 被阅读78次

    一些说明:
    最近在南京某公司那边实习,和自己在学校里不同的是:学校里基本都是独立开发,为了提高开发效率我一般采用storyBoard进行开发,没有接触过Masonry这个三方库,实现cell高度自适应都是计算的frame,然而昨天让我写一个页面,要求纯代码,遇到了cell高度自适应的问题,于是研究了下。

    话不多说直接上代码

    //在tableView代理方法中
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        static TemplateListCell *cell = nil;
        static dispatch_once_t onceToken;
        
        dispatch_once(&onceToken, ^{
            cell = [self.tableView dequeueReusableCellWithIdentifier:TemplateListCellIdentifier];
        });
        
        //这个是cell的初始化方法,里面包含了一些赋值操作
        [cell configureCellWithItem:self.viewModel.dataSource[indexPath.row]];
        
        //自定义计算高度的方法
        return [self calculateHeightForConfiguredSizingCell:cell];
    }
    
    - (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell {
        [sizingCell layoutIfNeeded];
        
        //调用系统提供的systemLayoutSizeFittingSize API进行高度计算
        CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        return size.height;
    }
    
    

    在TemplateListCell中实现configureCellWithItem方法

    -(void)configureCellWithItem:(id)item{
        if (![item isKindOfClass:[TemplateModel class]]) {
            return;
        }
        
        self.templateModel = (TemplateModel *)item;
        self.titleLabel.text = self.templateModel.template_name;
        self.contentLabel.text = self.templateModel.template_content;
      
        [self setupSubView];
    }
    
    //contentLabel的setter方法
    -(UILabel *)contentLabel{
        if (!_contentLabel) {
            _contentLabel = [[UILabel alloc]init];
            _contentLabel.textColor = [UIColor colorWithRed:180/255.0 green:180/255.0 blue:180/255.0 alpha:1];
            _contentLabel.font = [UIFont systemFontOfSize:14];
            _contentLabel.textAlignment = NSTextAlignmentLeft;
            _contentLabel.numberOfLines = 0;
            _contentLabel.textColor = HexRGB(0x8E8E93);
            //设置label的换行模式
            [_contentLabel setLineBreakMode:NSLineBreakByWordWrapping];
            //这里的67是根据label的左右约束计算出来的
            [_contentLabel setPreferredMaxLayoutWidth:APP_SCREEN_WIDTH - 67];
    
        }
        return _contentLabel;
    }
    
    -(void)setupSubView{
       //进行一些布局操作
       [self.contentView addSubview:self.contentLabel];
       
      [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(8);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-13);
            //左边约束
            make.left.mas_equalTo(self.contentView.mas_left).offset(15);
            //右边约束
            make.right.mas_equalTo(self.contentView.mas_right).offset(-52);
        }];
    }
    
    
    
    

    以上只是贴出了部分核心的代码,有些代码省略了

    效果图

    IMG_5192.PNG

    相关文章

      网友评论

        本文标题:iOS 在使用Masonry的情况下实现tableViewCel

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