gpt4 book ai didi

ios - 使用 Masonry 自动布局 UITableViewCell

转载 作者:行者123 更新时间:2023-11-28 21:40:58 26 4
gpt4 key购买 nike

我正在尝试使用自动布局自动调整表格单元格的大小。但是 TableView 似乎忽略了高度限制。

在UItableviewCell中:

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

[self.icon mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(self.contentView).with.offset(10);
make.left.equalTo(self.contentView).with.offset(10);
make.width.equalTo(@70);
// make.height.equalTo(@70);
make.bottom.equalTo(self.contentView.mas_bottom).with.offset(10);
}];
}
}

在 UITableView 中

XLTableViewCell * _cell = [[XLTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"];



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

CGSize size = [_cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
NSLog(@"%f",size.height);

// size.height is 0.0f;
return size.height + 1;
}

我正在尝试在 UITableViewCell 中创建图像 (70x70)。 image.top = 10, image.left = 10,image.bottom = 10.

最佳答案

每当我们向具有可变高度的组件添加任何约束时,不要从底部绑定(bind)它。只需添加一些常量高度约束并更新常量值即可。

make.height.constant = 一些值

并更新 View 约束

使用其中之一

  • (void)updateConstraintsIfNeeded NS_AVAILABLE_IOS(6_0);//从底部向上更新以接收器为根的 View 层次结构的约束。 UIWindow 的实现会在必要时首先创建一个布局引擎。
  • (void)updateConstraints NS_AVAILABLE_IOS(6_0);//覆盖它以在约束更新过程中调整您的特殊约束
  • (BOOL)needsUpdateConstraints NS_AVAILABLE_IOS(6_0);
  • (void)setNeedsUpdateConstraints NS_AVAILABLE_IOS(6_0);

关于ios - 使用 Masonry 自动布局 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32086283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com