动态的UITableView单元格高度与自动版式与动态类型标签 [英] Dynamic UITableView cell height with AutoLayout with Dynamic Type Labels

查看:175
本文介绍了动态的UITableView单元格高度与自动版式与动态类型标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自动版式显示的图像和2的标签,使用动态类型自适应字号实施我的tableview细胞。

I'm implementing my tableview cells using AutoLayout to show an image and 2 labels, using Dynamic Type for adaptive font size.

我实现estimatedHeightForRowAtIndexPath,这是非常合情合理的,易于使用。

I implemented estimatedHeightForRowAtIndexPath, which makes perfect sense and is easy to use.

我不使用Interface Builder的细胞。相反,我用砖石,这不应该使任何区别。

I don't use interface builder for the cells. Instead I use Masonry, which shouldn't make any difference.

我目前正在计算实际单元格高度挣扎。手工计算这是一个痛苦和艰难更新自动布局code时维护。

I'm currently struggling with calculating the actual cell height. Calculating it manually is a pain and difficult to maintain when updating the auto layout code.

我发现这个StackOverflow的答案:<一href=\"http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-heights\">Using自动布局中的UITableView动态单元布局和放大器; 的高度
该解决方案还应该照顾不同的字体大小,但对我不起作用。

I found this StackOverFlow answer: Using Auto Layout in UITableView for dynamic cell layouts & heights This solution should also take care of the different font sizes, but doesn't work for me.

在我有自动版式系统这样的:用的UILabel填充到顶部内容查看,另外的UILabel与填充内容查看底部,它会自动计算出细胞高度

When I have the AutoLayout system like that: UILabel to top contentView with padding, another UILabel to bottom of contentView with padding, it should automatically calculate the cell height.

但是,相反,它会导致以下冲突自动版式:

But instead, it results in the following AutoLayout conflict:

UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<MASLayoutConstraint:0xc506be0 UIImageView:0xc5070a0.height == 150>",
    "<MASLayoutConstraint:0xc506d90 UIImageView:0xc5070a0.top == UITableViewCellContentView:0xc5076e0.top + 10>",
    "<MASLayoutConstraint:0xc506990 UILabel:0xc507450.top == UIImageView:0xc5070a0.bottom + 10>",
    "<MASLayoutConstraint:0xc506630 UILabel:0xc507260.top == UILabel:0xc507450.bottom>",
    "<MASLayoutConstraint:0xc506530 UILabel:0xc507260.bottom == UITableViewCellContentView:0xc5076e0.bottom>",
    "<NSAutoresizingMaskLayoutConstraint:0xc3d05a0 UITableViewCellContentView:0xc5076e0.height == 44>"
)

我用下面的自动版式code:

I use the following AutoLayout code:

[self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom);
            make.right.equalTo(self.contentView.mas_right).with.offset(-GCBaconCellRowPadding);
            make.left.equalTo(self.contentView.mas_left).with.offset(GCBaconCellRowPadding);
            make.bottom.equalTo(self.contentView.mas_bottom);
        }];

最后一行应表明,电池的高度延伸本身

The last line should indicate that the height of the cell extends itself.

综观自动版式冲突的输出,它看起来像它要自动设置高度44.0,这是缺省值。

Looking at the output of the AutoLayout conflict, it looks like it wants to set the height automatically to 44.0, which is the default value.

修改
设置内容查看的translatesAutoresizingMaskIntoConstraints为NO

Edit: Setting the contentView's translatesAutoresizingMaskIntoConstraints to NO

self.contentView.translatesAutoresizingMaskIntoConstraints = NO;

创建细胞修复碰撞,但会导致零行高度时。

when creating the cell fixes the collision, but results in a row height of zero.

推荐答案

我最终使用以下code:

I ended up using the following code:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static GCBaconCell *offscreenCell;

    if (!offscreenCell)
    {
        offscreenCell = [[GCBaconCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                    reuseIdentifier:@"nothing"];
    }


    // configure offscreenCell ...

    [offscreenCell.contentView setNeedsLayout];
    [offscreenCell.contentView layoutIfNeeded];

    CGSize maximumSize = CGSizeMake(320.0, UILayoutFittingCompressedSize.height);
    CGFloat height = [offscreenCell.contentView systemLayoutSizeFittingSize:maximumSize].height;

    return height;
}

在控制器中。

在单元格视图确保使用下面的行

In the cell view make sure to use the following line

self.contentView.translatesAutoresizingMaskIntoConstraints = NO;

这篇关于动态的UITableView单元格高度与自动版式与动态类型标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆