Masonry和UITableView-FDTemplateLayoutCell的结合使用

     以前一直是用模型里声明一个高度属性,在cell中进行计算然后再返回到tableview中,最近发现有个比较好用的计算cell高度的库,没错就是UITableView-FDTemplateLayoutCell,附上git地址 https://github.com/forkingdog/UITableView-FDTemplateLayoutCell,感谢大牛!项目中一直使用Masonry进行纯代码适配,所以和UITableView-FDTemplateLayoutCell配合了一波,在这里跟大家分享一下。

     首先创建自定义的Cell,这里我创建了两个UILabel控件。
    _titleLabel = [UILabel new];
    _titleLabel.numberOfLines = 0;
    _titleLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(10);
        make.top.equalTo(self.contentView.mas_top).offset(15);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
    }];

    _contentLabel = [UILabel new];
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(10);
        make.top.equalTo(_titleLabel.mas_bottom).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
     接下来是初始化一个UITableView了。
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 320, 568)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    //要在这里进行cell的注册
    [_tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCell"];
     需要注意的是必须要有这句“[_tableView registerClass:[MyCell class] forCellReuseIdentifier:@"MyCell"];”进行cell的注册,否则可能会crash。

     然后实现UITableView的代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return yourDataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(MyCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    //使用Masonry进行布局的话,这里要设置为NO
    cell.fd_enforceFrameLayout = NO; 

    cell.mode = self.yourDataArr[indexPath.row];
}
     要把配置cell的数据或其他属性的代码分离出来,因为一会高度计算的时候也会用到,这样就不会有重复代码了。

     最后是关键的计算高度了。UITableView-FDTemplateLayoutCell为我们提供了三种计算高度的方法,我都写在了下边。
   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //普通的计算高度,不进行缓存
    return [tableView fd_heightForCellWithIdentifier:@"MyCell" configuration:^(id cell) {
        [self configureCell:cell atIndexPath:indexPath];
    }];

    //根据数据源里你的自定key进行缓存高度
    return [tableView fd_heightForCellWithIdentifier:@"MyCell" cacheByKey:@"yourKey" configuration:^(id cell) {
        [self configureCell:cell atIndexPath:indexPath];
    }];

    //根据indexPath进行缓存高度
    return [tableView fd_heightForCellWithIdentifier:@"MyCell" cacheByIndexPath:indexPath configuration:^(id cell) {
        [self configureCell:cell atIndexPath:indexPath];
    }];

} 
     调试的时候你可以 “_tableView.fd_debugLogEnabled = YES;” 打开调试日志进行调试。

     核心代码就这么多了,有问题的小伙伴可以评论提出来,大家一起解决,共同进步!

你可能感兴趣的:(iOS)