美文网首页
iOS8 Masonry UITableViewCell 自适

iOS8 Masonry UITableViewCell 自适

作者: Lee_Jo | 来源:发表于2017-07-04 12:01 被阅读0次

    iOS8中实现了UITableViewCell 高度自适应方式 简化了自适应高度代码。

    1:初始化tableView

    - (void)viewDidLoad {

    [super viewDidLoad];

    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];

    _tableView.dataSource = self;

    _tableView.delegate = self;

    _tableView.backgroundColor = [UIColor grayColor];

    _tableView.estimatedRowHeight = 120;

    _tableView.rowHeight = UITableViewAutomaticDimension;

    [self.view addSubview:_tableView];

    }

    这里 _tableView.estimatedRowHeight = 120;和_tableView.rowHeight = UITableViewAutomaticDimension;是实现自适应高度的关键。第一个意思是预设高度。这个高度和实现的高度尽量差距小可以保证速度更快

    2: 实现UITableViewDataSource 与 UITableViewDelegate 协议

    这里是常见的实现方式 需要注意的是

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 

    这个方法不能实现。

    3: 自定义Cell 实现自适应高度的关键在  cell的contentView的上下左右约束必须定义。意思就是说自适应的约束条件是依赖cell的contentView的上下左右约束。这也是其关键 以下为一自定义cell使用示例

    #import "TBSessionTableViewCell.h"

    #import "Masonry.h"

    #import "TBEntity.h"

    #define TagImgHead 1000

    #define TagTitle 1001

    #define TagDetail 1002

    @implementation TBSessionTableViewCell

    - (void)bindData:(TBEntity*)entity indexPath:(NSIndexPath*)indexPath {

    __weak TBSessionTableViewCell *weakSelf = self;

    UIImageView *imageViewHead = [self.contentView viewWithTag:TagImgHead];

    if(nil == imageViewHead) {

    imageViewHead = [[UIImageView alloc] init];

    imageViewHead.tag = TagImgHead;

    imageViewHead.backgroundColor = [UIColor clearColor];

    [self.contentView addSubview:imageViewHead];

    }

    imageViewHead.image = [UIImage imageNamed:@"boy_head_img"];

    UILabel *labelTitel = [self.contentView viewWithTag:TagTitle];

    if (nil == labelTitel) {

    labelTitel  = [[UILabel alloc] init];

    labelTitel.tag = TagTitle;

    labelTitel.backgroundColor = [UIColor greenColor];

    labelTitel.font = [UIFont systemFontOfSize:28];

    [self.contentView addSubview:labelTitel];

    }

    labelTitel.text = entity.title;

    UITextView *textView = [self.contentView viewWithTag:TagDetail];

    if (nil == textView) {

    textView = [[UITextView alloc] init];

    textView.tag = TagDetail;

    textView.font = [UIFont systemFontOfSize:22];

    textView.backgroundColor = [UIColor blueColor];

    textView.userInteractionEnabled = NO;

    textView.scrollEnabled = NO;

    [textView setShowsVerticalScrollIndicator:NO];

    [textView setShowsHorizontalScrollIndicator:NO];

    [self.contentView addSubview:textView];

    }

    textView.text = entity.detail;

    [imageViewHead mas_remakeConstraints:^(MASConstraintMaker *make) {

    make.left.equalTo(weakSelf.contentView.mas_left).mas_equalTo(10);

    make.top.equalTo(weakSelf.contentView.mas_top).mas_equalTo(10);

    make.width.equalTo(@60);

    make.height.equalTo(@60);

    make.bottom.mas_lessThanOrEqualTo(@-10);//保证底部至少与图片有10的距离

    }];

    [labelTitel mas_remakeConstraints:^(MASConstraintMaker *make) {

    make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(10);

    make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(80);

    make.width.mas_equalTo(weakSelf.contentView.frame.size.width-80-10);

    make.height.equalTo(@28);

    }];

    CGFloat fW = weakSelf.contentView.frame.size.width-80-10;

    //计数字高度

    CGSize contentSize = [textView.text boundingRectWithSize:CGSizeMake(fW, MAXFLOAT)

    options:NSStringDrawingUsesLineFragmentOrigin |

    NSStringDrawingTruncatesLastVisibleLine |

    NSStringDrawingUsesFontLeading

    attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:22.0]}

    context:nil].size;

    [textView mas_remakeConstraints:^(MASConstraintMaker *make) {

    make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(80);

    make.top.equalTo(@58);

    make.width.mas_equalTo(fW);

    make.height.mas_equalTo(contentSize.height);

    make.bottom.mas_lessThanOrEqualTo(@-10).priorityLow();//处理约束冲突警告保证底部10距离

    }];

    }

    相关文章

      网友评论

          本文标题:iOS8 Masonry UITableViewCell 自适

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