TableView高度自适应(Masonry约束)

高度自适应需要TableView设置为自动适配高度,cell添加约束,约束没问题,高度自适应就不会出问题:

-UITableViewAutomaticDimension设置为高度自适应
- Masonry添加约束(也可以用xib)


TableVIew设置为自动适配高度

_ (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{

//自动计算行高
return UITableViewAutomaticDimension;

}

masonry约束

.m文件(cell)

[_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.contentView).equalTo(@0);
    make.left.equalTo(self.contentView).equalTo(@0);
    make.right.equalTo(self.contentView).equalTo(@0);
    make.height.equalTo(@200);
}];

   [_lable mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.imgView.mas_bottom).equalTo(@20);
    make.left.equalTo(self.contentView).equalTo(@20);
    make.right.equalTo(self.contentView).equalTo(@-20);
    make.bottom.equalTo(self.timeLable.mas_top).equalTo(@-20);
}];

   [_timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.contentView).equalTo(@20);
    make.width.equalTo(@100);
    make.bottom.equalTo(self.contentView).equalTo(@-20);
}];

   [_buttou mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.timeLable).equalTo(@0);
    make.left.equalTo(self.timeLable.mas_right).equalTo(@20);
    make.width.equalTo(@60);
    make.bottom.equalTo(self.contentView).equalTo(@-20);
}];

   [_other mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.timeLable).equalTo(@0);
    make.left.equalTo(self.buttou.mas_right).equalTo(@20);
    make.width.equalTo(@60);
    make.bottom.equalTo(self.contentView).equalTo(@-20);
}];

重要代码

viewController.m代码
#
- (void)creatView
{

self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];

[_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(self.view);
    make.height.equalTo(self.view);
}];

}

#
-(NSMutableDictionary *)heightAtIndexPath
{

if (!_heightAtIndexPath) {
    _heightAtIndexPath = [NSMutableDictionary dictionary];
}
return _heightAtIndexPath;

}
#
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 1;

}
#
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return _arrayData.count;

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

ViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"CellID%ld",(long)indexPath.row]];
if (!cell) {
    cell = [[ViewTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"CellID%ld",(long)indexPath.row]];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
   cell.imgView.image = [UIImage imageNamed:@"image"];
   cell.lable.text = _arrayData[indexPath.row];
   cell.timeLable.text = @"2018.09.10";

   cell.touchBlock = ^{
    [self.arrayData replaceObjectAtIndex:indexPath.row withObject:[NSString stringWithFormat:@"点击了第%d行",indexPath.row]];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
};

   cell.removeBlock = ^{
    [self.arrayData removeObjectAtIndex:indexPath.row];
    [self.heightAtIndexPath removeAllObjects];
    [self.tableView reloadData];
};
return cell;

}

pragma mark - UITableViewDelegate

-(CGFloat)tableView:(UITableView )tableView estimatedHeightForRowAtIndexPath:(NSIndexPath )indexPath
{

//判断是否有缓存好的高度,有直接赋值,没有就给一个高度(这个高度随便写,会自动适配好高度)
NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
if(height)
{
    return height.floatValue;
}
else
{
    return 100;
}

}
#
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{

//自动计算行高
return UITableViewAutomaticDimension;

}
#
- (void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

//cell初始化完成,缓存cell的高度到字典中
NSNumber *height = @(cell.frame.size.height);
[self.heightAtIndexPath setObject:height forKey:indexPath];

}

ViewTableViewCell.m

#
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    [self initLayout];
}
return self;

}
#
- (void)initLayout
{

_imgView = [[UIImageView alloc]init];
_imgView.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:_imgView];

_lable = [[UILabel alloc]init];
_lable.textColor = [UIColor blackColor];
_lable.font = [UIFont systemFontOfSize:14];
_lable.textAlignment = NSTextAlignmentLeft;
_lable.backgroundColor = [UIColor whiteColor];
_lable.numberOfLines = 0;
[self.contentView addSubview:_lable];

_timeLable = [[UILabel alloc]init];
_timeLable.textColor = [UIColor blackColor];
_timeLable.font = [UIFont systemFontOfSize:14];
_timeLable.textAlignment = NSTextAlignmentLeft;
_timeLable.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:_timeLable];

_buttou = [UIButton buttonWithType:UIButtonTypeCustom];
[_buttou setTitle:@"点击" forState:UIControlStateNormal];
[_buttou addTarget:self action:@selector(touchBtnDemo) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_buttou];

_other = [UIButton buttonWithType:UIButtonTypeCustom];
[_other setTitle:@"删除" forState:UIControlStateNormal];
[_other setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_other addTarget:self action:@selector(touchOtherDemo) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_other];

[_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.contentView).equalTo(@0);
    make.left.equalTo(self.contentView).equalTo(@0);
    make.right.equalTo(self.contentView).equalTo(@0);
    make.height.equalTo(@200);
}];

[_lable mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.imgView.mas_bottom).equalTo(@20);
    make.left.equalTo(self.contentView).equalTo(@20);
    make.right.equalTo(self.contentView).equalTo(@-20);
    make.bottom.equalTo(self.timeLable.mas_top).equalTo(@-20);
}];

[_timeLable mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.contentView).equalTo(@20);
    make.width.equalTo(@100);
    make.bottom.equalTo(self.contentView).equalTo(@-20);
}];

[_buttou mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.timeLable).equalTo(@0);
    make.left.equalTo(self.timeLable.mas_right).equalTo(@20);
    make.width.equalTo(@60);
    make.bottom.equalTo(self.contentView).equalTo(@-20);
}];

[_other mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.timeLable).equalTo(@0);
    make.left.equalTo(self.buttou.mas_right).equalTo(@20);
    make.width.equalTo(@60);
    make.bottom.equalTo(self.contentView).equalTo(@-20);
}];

}
#
- (void)touchBtnDemo
{

if (self.touchBlock) {
    self.touchBlock();
}

}
#
- (void)touchOtherDemo
{

if (self.removeBlock) {
    self.removeBlock();
}

}

重要代码都在这里了

你可能感兴趣的:(TableView高度自适应(Masonry约束))