iOS--Masonry测试UITableViewCell自适应

2021-07-18  本文已影响0人  mayuee

ViewController里实现

#import "Masonry.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)NSMutableArray *dataArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < 20; i++) {
        if (i%3 == 0) {
            [self.dataArray addObject:@"这是一个长长长长长长长长长长长长长长长长标题"];
        }
        else if (i%3 == 1){
            [self.dataArray addObject:@"这是一个长长长长长长长长长长长长长长长长长长长长长长长长长长长长标题"];
        }
        else{
            [self.dataArray addObject:@"这是短标题"];
        }
    }

    UITableView *tableView = [[UITableView alloc] init];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.view.mas_height);
        make.width.mas_equalTo(self.view.mas_width);
    }];
    
    //设行高为自动计算
    tableView.rowHeight = UITableViewAutomaticDimension;
    //预计行高
    tableView.estimatedRowHeight = 60;

    [self.view addSubview:tableView];
}

#pragma mark- tableViewDelegate
#pragma mark tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"TKMarginTradeSecurities";
    
    KTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[KTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSString *title = [self.dataArray objectAtIndex:indexPath.row];
    [cell setSubTitle:title];
    [cell layoutIfNeeded];

    return cell;
}

//- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 60;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewAutomaticDimension;
//}

自定义CELL

@interface KTableViewCell : UITableViewCell

- (void)setSubTitle:(NSString *)title;

@end

#import "KTableViewCell.h"
#import "Masonry.h"

@interface KTableViewCell()

@property (nonatomic,strong)UILabel *label_type;
@property (nonatomic,strong)UILabel *label_name;
@property (nonatomic,strong)UILabel *label_content;
@property (nonatomic,strong)UILabel *label_money;
@property (nonatomic,strong)UILabel *label_status;

@end


@implementation KTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       [self.contentView addSubview:self.label_type];
       [self.contentView addSubview:self.label_name];
       [self.contentView addSubview:self.label_content];
       [self.contentView addSubview:self.label_money];
       [self.contentView addSubview:self.label_status];
   }
   return self;
}

- (void)layoutSubviews
{
   [super layoutSubviews];
   
   [self.label_type mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.contentView.mas_left).offset(15);
       make.top.equalTo(self.contentView.mas_top).offset(12);
       make.width.mas_equalTo(30);
       make.height.mas_equalTo(18);
   }];
   
   [self.label_name mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.label_type.mas_right).offset(5);
       make.top.mas_equalTo(10);
       make.right.equalTo(self.label_money.mas_left);
   }];
   
   [self.label_money mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.mas_equalTo(10);
       make.right.equalTo(self.contentView.mas_right).offset(-15);
       make.width.mas_equalTo(100);
       make.height.mas_equalTo(18);
   }];
   
   [self.label_content mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.label_name.mas_left);
       make.top.equalTo(self.label_name.mas_bottom).offset(10);
       make.right.equalTo(self.label_name.mas_right);
       make.bottom.equalTo(self.contentView).offset(-10);
   }];
   
   [self.label_status mas_makeConstraints:^(MASConstraintMaker *make) {
       make.right.equalTo(self.contentView.mas_right).offset(-15);
       make.height.equalTo(self.label_content.mas_height);
       make.width.mas_equalTo(80);
       make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
   }];
}

- (UILabel *)label_type
{
   if (!_label_type) {
       _label_type = [[UILabel alloc]  init];
       _label_type.text = @"哈哈";
       _label_type.font = [UIFont systemFontOfSize:12];
       _label_type.textAlignment = NSTextAlignmentCenter;
       _label_type.layer.borderWidth = 0.5;
       _label_type.layer.borderColor = [UIColor redColor].CGColor;
       _label_type.textColor = [UIColor blueColor];
   }
   return _label_type;
}

- (UILabel *)label_name
{
   if (!_label_name) {
       _label_name = [[UILabel alloc] init];
       _label_name.text = @"--";
       _label_name.font = [UIFont systemFontOfSize:17.f];
       _label_name.numberOfLines = 0;
//        _label_name.lineBreakMode = NSLineBreakByWordWrapping;
       _label_name.textColor = [UIColor blackColor];
   }
   return _label_name;
}

- (UILabel *)label_content
{
   if (!_label_content) {
       _label_content = [[UILabel alloc]  init];
       _label_content.text = @"这里是个描述";
       _label_content.font = [UIFont systemFontOfSize:12];
       _label_content.textAlignment = NSTextAlignmentLeft;
       _label_content.textColor = [UIColor lightGrayColor];
   }
   return _label_content;
}

- (UILabel *)label_money
{
   if (!_label_money) {
       _label_money = [[UILabel alloc]  init];
       _label_money.text = @"12345.7";
       _label_money.font = [UIFont systemFontOfSize:17.f];
       _label_money.textAlignment = NSTextAlignmentRight;
       _label_money.textColor = [UIColor lightGrayColor];
   }
   return _label_money;
}

- (UILabel *)label_status
{
   if (!_label_status) {
       _label_status = [[UILabel alloc]  init];
       _label_status.text = @"这是状态";
       _label_status.font = [UIFont systemFontOfSize:12];
       _label_status.textAlignment = NSTextAlignmentRight;
       _label_status.textColor = [UIColor lightGrayColor];
   }
   return _label_status;
}

- (void)setSubTitle:(NSString *)title;
{
   self.label_name.text = title;
}

@end

效果

效果图
上一篇下一篇

猜你喜欢

热点阅读