cell高度自适应

在这里插入代码片#import "JHHViewController.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "JHHTableViewCell.h"
#define  WIDTH [UIScreen mainScreen].bounds.size.width
#define  HEIGHT [UIScreen mainScreen].bounds.size.height
@interface JHHViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * tableView;
@property(nonatomic,strong) NSArray * dataSource;
@end
@implementation JHHViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setNav];
    self.dataSource = @[@"毛泽东(1893年12月26日-1976年9月9日),字润之(原作咏芝,后改润芝),笔名子任。湖南湘潭人。中国人民的领袖,马克思主义者,伟大的无产阶级革命家、战略家和理论家,中国共产党、中国人民解放军和中华人民共和国的主要缔造者和领导人,诗人,书法家。",@"1949至1976年,毛泽东担任中华人民共和国最高领导人。他对马克思列宁主义的发展、军事理论的贡献以及对共产党的理论贡献被称为毛泽东思想。因毛泽东担任过的主要职务几乎全部称为主席,所以也被人们尊称为“毛主席”。",@"毛泽东,湖南湘潭人。1893年12月26日生于一个农民家庭。辛亥革命爆发后在起义的新军中当了半年兵。1914~1918年,在湖南第一师范学校求学。毕业前夕和蔡和森等组织革命团体新民学会。五四运动前后接触和接受马克思主义,1920年11月,在湖南创建共产主义组织。1921年7月,出席中国共产党第一次全国代表大会,后任中共湘区委员会书记,领导长沙、安源等地工人运动。1923年6月,出席中共“三大”,被选为中央执行委员,参加中央领导工作。1924年1月国共合作后,在国民党第一、第二次全国代表大会上都当选为候补中央执行委员,曾在广州任国民党中央宣传部代理部长,主编《政治周报》,主办第六届农民运动讲习所。1926年11月,任中共中央农民运动委员会书记。",@"1925年冬至1927年春,先后发表《中国社会各阶级的分析》、《湖南农民运动考察报告》等著作,指出农民问题在中国革命中的重要地位和无产阶级领导农民斗争的极端重要性,批评了陈独秀的右倾思想。",@"抗日战争胜利后,针对蒋介石企图消灭共产党及其武装力量的现实,他提出“针锋相对”的斗争方针。",@"1946年夏蒋介石发动全面内战后,毛泽东同朱德、周恩来领导中国人民解放军进行积极防御,集中优势兵力,各个歼灭敌人。1947年3月至1948年3月,同周恩来、任弼时转战陕北,指挥西北战场和全国的解放战争。1947年夏,中国人民解放军从战略防御转入战略进攻,在以他为首的党中央领导下,经过辽沈、淮海、平津三大战役和1949年4月的渡江战役,推翻了国民党政府。1949年3月,主持召开中共七届二中全会,并作重要报告,决定把党的工作重心从农村转到城市,规定了党在全国胜利以后的各项基本政策,号召全党务必保持谦虚、谨慎、不骄、不躁的作风,务必继续保持艰苦奋斗的作风。7月1日,发表《论人民民主专政》,规定了人民共和国的政权的性质及其对内对外的基本政策。"];
    [self.view addSubview:self.tableView];
    
}
#pragma maek -- 设置导航条
-(void)setNav{
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"JHH-SDAutoLayout";
    self.navigationController.navigationBar.translucent = NO;
}
#pragma mark -- 懒加载创建TableView
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStylePlain];
        _tableView.dataSource =self;
        _tableView.delegate = self;
        [_tableView registerClass:[JHHTableViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}
#pragma mark -- 表格协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JHHTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.contentLabel.text = _dataSource[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    //一句话搞定cell高度自适应
    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:WIDTH tableView:tableView];
}
@end

@property(nonatomic,strong) UIImageView *iconImageV;
@property(nonatomic,strong) UILabel * titleLabel;
@property(nonatomic,strong) UILabel * contentLabel;


#import "JHHTableViewCell.h"
#import "SDAutoLayout.h"
@implementation JHHTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle: style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.iconImageV = [[UIImageView alloc]init];
        self.iconImageV.image  = [UIImage imageNamed:@"1"];
        
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.text = @"我是标题";
        self.contentLabel = [[UILabel alloc]init];
        
        [self.contentView addSubview:self.iconImageV];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contentLabel];
        
        self.iconImageV.sd_layout
        .widthIs(50)
        .heightIs(50)
        .topSpaceToView(self.contentView, 10)
        .leftSpaceToView(self.contentView, 10);
        
        self.titleLabel.sd_layout
        .topSpaceToView(self.contentView, 10)
        .leftSpaceToView(self.iconImageV, 10)
        .rightSpaceToView(self.contentView, 10)
        .heightIs(20);
        
        self.contentLabel.sd_layout
        .leftSpaceToView(self.iconImageV, 8)
        .topSpaceToView(self.titleLabel, 5)
        .rightSpaceToView(self.contentView, 10)
        .autoHeightRatio(0);
        
        [self setupAutoHeightWithBottomView:self.contentLabel bottomMargin:10];
        
    
        
    }
    return self;
}
@end

猜你喜欢

转载自blog.csdn.net/chuck_phonics/article/details/85039782
今日推荐