相关疑难解决方法(0)

在UITableView中使用自动布局来获取动态单元格布局和可变行高

如何UITableViewCell在表格视图中使用s 内的自动布局,让每个单元格的内容和子视图确定行高(本身/自动),同时保持平滑的滚动性能?

row-height uitableview ios autolayout nsautolayout

1477
推荐指数
18
解决办法
55万
查看次数

具有动态类型标签的AutoLayout的动态UITableView单元格高度

我正在使用AutoLayout实现我的tableview单元格来显示图像和2个标签,使用动态类型来显示自适应字体大小.

我实现了estimatedHeightForRowAtIndexPath,它非常有意义且易于使用.

我没有为单元格使用界面构建器.相反,我使用砌体,这应该没有任何区别.

我目前正在努力计算实际的细胞高度.在更新自动布局代码时,手动计算是一件痛苦的事情并且难以维护.

我发现这个StackOverFlow答案:在UITableView中使用自动布局来获得动态单元格布局和可变行高度 此解决方案还应该考虑不同的字体大小,但对我来说不起作用.

当我有这样的AutoLayout系统时:UILabel顶部带有填充的contentView,另一个UILabel到带有填充的contentView底部,它应该自动计算单元格高度.

但相反,它会导致以下AutoLayout冲突:

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>"
)
Run Code Online (Sandbox Code Playgroud)

我使用以下AutoLayout代码:

[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);
        }];
Run Code Online (Sandbox Code Playgroud)

最后一行应表明单元格的高度自身延伸.

查看AutoLayout冲突的输出,看起来它想要自动将高度设置为44.0,这是默认值.

编辑:将contentView的translatesAutoresizingMaskIntoConstraints设置为NO

self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)

创建单元格时会修复碰撞,但会导致行高为零.

objective-c uitableview ios autolayout

2
推荐指数
1
解决办法
9415
查看次数