zoukankan      html  css  js  c++  java
  • forkingdog 和 masonry 实现自动计算行高

    #import "TableViewController.h"
    #import "fdModel.h"
    #import "fdTableViewCell.h"
    #import <MJExtension.h>
    #import "UITableView+FDTemplateLayoutCell.h"
    
    @interface TableViewController ()
    
    @property (nonatomic, copy) NSArray *fdArray;
    
    @end
    
    @implementation TableViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.tableView registerClass:[fdTableViewCell class] forCellReuseIdentifier:@"cell"];
    //    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.backgroundColor = [UIColor colorWithRed:231/255.0 green:231/255.0 blue:231/255.0 alpha:1];
    }
    
    #pragma mark - Table view data source
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        return self.fdArray.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        fdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.fdModel = self.fdArray[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        return [self.tableView fd_heightForCellWithIdentifier:@"cell" cacheByIndexPath:indexPath configuration:^(fdTableViewCell *cell) {
            cell.fdModel = self.fdArray[indexPath.row];
        }];
    }
    
    #pragma mark - lazyLoad
    - (NSArray *)fdArray {
        
        if (_fdArray == nil) {
            _fdArray = [[NSArray alloc] init];
            NSString *filePath = [[NSBundle mainBundle] pathForResource:@"microblog" ofType:@"plist"];
            _fdArray = [fdModel objectArrayWithFile:filePath];
        }
        return _fdArray;
    }
    

    cell  布局

    #import "fdTableViewCell.h"
    #import "fdModel.h"
    #import <Masonry.h>
    
    @interface fdTableViewCell ()
    
    @property (nonatomic, strong) UIView *publicView;
    
    @property (nonatomic, strong) UILabel *textLab;
    
    @property (nonatomic, strong) UILabel *nameLable;
    
    @property (nonatomic, strong) UIImageView *iconView;
    
    @property (nonatomic, strong) UIImageView *vipView;
    
    @property (nonatomic, strong) UIImageView *picView;
    
    @end
    
    @implementation fdTableViewCell
    
    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
        
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            
            [self createView];
            [self setViewAutoLayout];
        }
        return self;
    }
    
    - (void)createView {
        
        self.publicView = [[UIView alloc] init];
        [self.contentView addSubview:self.publicView];
        
        self.iconView = [[UIImageView alloc] init];
        [self.publicView addSubview:self.iconView];
        
        self.nameLable = [[UILabel alloc] init];
        [self.publicView addSubview:self.nameLable];
        
        self.vipView = [[UIImageView alloc] init];
        self.vipView.image = [UIImage imageNamed:@"vip"];
        [self.publicView addSubview:self.vipView];
        
        self.textLab = [[UILabel alloc] init];
        self.textLab.numberOfLines = 0;
        [self.publicView addSubview:self.textLab];
        
        self.picView = [[UIImageView alloc] init];
        [self.publicView addSubview:self.picView];
    }
    
    - (void)setViewAutoLayout {
        
        [self.publicView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(@10);
            make.left.equalTo(@10);
            make.right.equalTo(@-10);
    // 这里是重点。。。 make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10); }]; [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.left.equalTo(@10); make.width.equalTo(@44); make.height.equalTo(@44); }]; [self.nameLable mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.left.equalTo(self.iconView.mas_right).with.offset(10); }]; [self.nameLable sizeToFit]; [self.vipView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@10); make.left.equalTo(self.nameLable.mas_right).with.offset(10); make.height.equalTo(@15); make.width.equalTo(@15); }]; [self.textLab mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.iconView.mas_bottom).with.offset(10); make.left.equalTo(self.iconView); make.right.equalTo(@-20); }]; [self.picView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.textLab.mas_bottom).with.offset(10); make.left.equalTo(self.textLab); // 这个是重点。。。 make.bottom.equalTo(self.contentView); }]; } - (void)setFdModel:(fdModel *)fdModel { _fdModel = fdModel; self.iconView.image = [UIImage imageNamed:fdModel.icon]; self.nameLable.text = fdModel.name; self.vipView.hidden = (![fdModel.vip isEqual:@1]); self.textLab.text = fdModel.text; self.picView.image = [fdModel.picture length] > 0 ? [UIImage imageNamed:fdModel.picture] : nil; }
  • 相关阅读:
    socket编程原理
    配置Symbian WINS Emulator
    mysql 的乱码解决方法
    深入剖析关于JSP和Servlet对中文的处理
    一个分众传媒业务员的销售日记
    中移动第四季度SP评级结果出炉 A级企业仅5家
    基于socket的聊天室实现原理
    看Linux内核源码 练内力必备技能
    Dell要收购AMD?
    同步执行其他程序(dos命令)
  • 原文地址:https://www.cnblogs.com/yangzhifan/p/4901678.html
Copyright © 2011-2022 走看看