美文网首页
UITableView+FDTemplateLayoutCell

UITableView+FDTemplateLayoutCell

作者: _沒看懂 | 来源:发表于2019-01-22 18:00 被阅读0次

最近写了一个评论页面用到了UITableView+FDTemplateLayoutCell框架,先说下这个框架对于自动计算行高非常好用,项目里用到了一个组HeaderFooterView页需要自动计算高度,刚好框架了就有一个方法返回组视图的高度,十分开心的使用了

- (CGFloat)fd_heightForHeaderFooterViewWithIdentifier:(NSString *)identifier configuration:(void (^)(id headerFooterView))configuration;

但是发现约束过后竟然算不出高度,后来翻看源码发现,作者的回调BLock没有返回,导致数据没有进行刷新,所以计算的高度也不对。需要在源码里加入一行,搞定又可以开心的使用了

if (configuration) {
        configuration(templateHeaderFooterView);
 }
- (CGFloat)fd_heightForHeaderFooterViewWithIdentifier:(NSString *)identifier configuration:(void (^)(id))configuration {
    UITableViewHeaderFooterView *templateHeaderFooterView = [self fd_templateHeaderFooterViewForReuseIdentifier:identifier];
    >>>>mark--- 这里加一行代码,把视图返回出去,就可以正常计算了
    if (configuration) {
        configuration(templateHeaderFooterView);
    }
    
    NSLayoutConstraint *widthFenceConstraint = [NSLayoutConstraint constraintWithItem:templateHeaderFooterView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:CGRectGetWidth(self.frame)];
    [templateHeaderFooterView addConstraint:widthFenceConstraint];
    CGFloat fittingHeight = [templateHeaderFooterView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    [templateHeaderFooterView removeConstraint:widthFenceConstraint];
    
    if (fittingHeight == 0) {
        fittingHeight = [templateHeaderFooterView sizeThatFits:CGSizeMake(CGRectGetWidth(self.frame), 0)].height;
    }
    
    return fittingHeight;
}

相关文章

网友评论

      本文标题:UITableView+FDTemplateLayoutCell

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