美文网首页ios开发学习iOS开发(OC)
利用Masonry动态计算tableview-cell高度(OC

利用Masonry动态计算tableview-cell高度(OC

作者: 爱恨的潮汐 | 来源:发表于2017-04-19 17:58 被阅读1629次
【缺点就是利用自动布局计算行高很消耗cpu,每次滚动到该cell都要计算】
依赖框架:Masonry
一、控制器

#import "OCViewController.h"
#import "OCTableViewCell.h"
@interface OCViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * tableView;
//总数据
@property (nonatomic,strong) NSMutableArray *dataSouse;
@end

@implementation OCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"OC自适应高度";
    
    //数据
    NSArray * array = @[@"我就两个字",@"中新网4月17日电  据韩联社报道,韩国海洋水产部(简称“海水部”)“世越号”现场处理本部和负责世越号船体清理工作的韩国打捞局(Korea Salvage)为方便搜寻失踪者遗体的工作人员开展工作已于17日完成护栏安装,预计失踪者遗体搜寻工作有望于18日正式启动",@"这是第三个",@"3月28日,在将“世越”号船体运往木浦新港前,工作人员也同样在半潜船甲板上发现过动物尸骨。本月2日,工作人员曾在半潜船甲板上发现9块动物尸骨、“世越”号船长李某的护照及手提包、信用卡、圆珠笔等物品,但截至目前仍未发现9名失踪者的遗体。",@"",@"2014年4月16日,“世越”号在全罗南道珍岛郡附近水域沉没,共致295人遇难,迄今仍有9人下落不明,遇难者大多是学生。"];
    [self.dataSouse addObjectsFromArray:array];
    
    [self tableView];
}

//tableView懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0,  self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.view addSubview:_tableView];
        
        //开启自动计算高度
        //【重点】注意千万不要实现行高的代理方法,否则无效:heightForRowAt
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 44;
        
        

        
    }
    
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouse.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *identifierhot = @"cell";
    OCTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifierhot];
    if (!cell){
        cell = [[OCTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierhot];
        /* 忽略点击效果 */
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
    }
    
    cell.stringTitle = self.dataSouse[indexPath.row];
   
    return cell;

}



- (NSMutableArray *)dataSouse{
    
    if (_dataSouse == nil) {
        _dataSouse = [NSMutableArray array];
    }
    return _dataSouse;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

二、cell层

#import <UIKit/UIKit.h>

@interface OCTableViewCell : UITableViewCell
@property(nonatomic,strong)NSString * stringTitle;
@end

#import "OCTableViewCell.h"
//自动布局
#import "Masonry.h"

@interface OCTableViewCell()


@property (weak, nonatomic)  UILabel *labeltitle;//标题
@property (weak, nonatomic)  UIImageView *pictureView;//大图
@property (weak, nonatomic)UILabel *labelContont;//内容

@end

@implementation OCTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setupUI];
      
        
    }
    return self;
}

//赋值
-(void)setStringTitle:(NSString *)stringTitle{
    _stringTitle = stringTitle;
    
    _labeltitle.text = stringTitle;
    _labelContont.text = stringTitle;
}

- (void)setupUI{
 
    //标题 【重点】必须在创建第一块控件的时候约束:contentView
    UILabel * labeltitle = [[UILabel alloc]init];
    labeltitle.backgroundColor = [UIColor whiteColor];
    self.labeltitle = labeltitle;
    labeltitle.text = @"标题";
    labeltitle.textAlignment = NSTextAlignmentLeft;
    labeltitle.numberOfLines = 0;
    [self.contentView addSubview:labeltitle];

    
    //大图
    UIImageView *pictureView = [[UIImageView alloc]init];
    self.pictureView = pictureView;
    pictureView.image = [UIImage imageNamed:@"IMG_0434.JPG"];
    pictureView.contentMode = UIViewContentModeScaleAspectFill;
    pictureView.clipsToBounds = YES;
    [self.contentView addSubview:pictureView];
    
    
    //内容
    UILabel * labelContont = [[UILabel alloc]init];
    labelContont.backgroundColor = [UIColor whiteColor];
    self.labelContont = labelContont;
    labelContont.text = @"内容";
    labelContont.textAlignment = NSTextAlignmentLeft;
    labelContont.numberOfLines = 0;
    labelContont.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actionbutton)];
    [labelContont addGestureRecognizer:tap];
    [self.contentView addSubview:labelContont];
    
    
    //标题约束
    [labeltitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(15);
        make.left.equalTo(self.contentView).with.offset(10);
        make.right.equalTo(self.contentView).with.offset(-10);
    }];
    
    //图片约束
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labeltitle.mas_bottom).with.offset(20);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(200);
        
    }];
    
    //设置内容约束  //内容 【重点】必须在创建最后一块控件的时候约束:contentView
    [labelContont mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(pictureView.mas_bottom).with.offset(20);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.equalTo(self.contentView).with.offset(-10);
        
    }];
    
    
}

-(void)Actionbutton{
    
    NSLog(@"点击了内容");
    
}

@end

github demo下载连接(包含OC版和Swift版):https://github.com/micheng23/tableView-cellHighly

相关文章

网友评论

  • PGOne爱吃饺子:楼主,这个方法有个缺点,每次滚动cell的时候,还会重新计算cell的高度吧
  • PGOne爱吃饺子:楼主这个方法太他妈好了,以前还要计算文字的高度,真麻烦啊,感谢楼主了

本文标题:利用Masonry动态计算tableview-cell高度(OC

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