gpt4 book ai didi

iphone - UITableViewCell 自动布局

转载 作者:搜寻专家 更新时间:2023-10-30 19:41:31 26 4
gpt4 key购买 nike

我有自动版式的问题。我有一个 UITableViewCell 和一个 View 。我希望此 View 小于单元格,因此我添加了约束:

  • 垂直空间(10)顶部
  • 垂直空间(10)底部
  • 水平空格(10)前导空格
  • 横向空格(10)尾随空格

当 tableView 出现时,单元格显示良好,但如果我向下滚动 tableView 并向上滚动,单元格中的 View 会调整大小,现在占据单元格中的全部空间。

正确的单元格: enter image description here

不正确的单元格(滚动后) enter image description here

我已经搜索过这个,但找不到任何类似的东西。我认为这个人有同样的问题,但没有答案: Similar problem

提前感谢您的帮助!

最佳答案

我终于找到了问题的答案 here .在我的自定义 UITableViewCell 类中,我这样做了:

- (void)awakeFromNib
{
[super awakeFromNib];

for (NSLayoutConstraint *cellConstraint in self.constraints)
{
[self removeConstraint:cellConstraint];

id firstItem = cellConstraint.firstItem == self ? self.contentView : cellConstraint.firstItem;
id seccondItem = cellConstraint.secondItem == self ? self.contentView : cellConstraint.secondItem;

NSLayoutConstraint* contentViewConstraint = [NSLayoutConstraint constraintWithItem:firstItem
attribute:cellConstraint.firstAttribute
relatedBy:cellConstraint.relation
toItem:seccondItem
attribute:cellConstraint.secondAttribute
multiplier:cellConstraint.multiplier
constant:cellConstraint.constant];

[self.contentView addConstraint:contentViewConstraint];
}
}

我猜内容 View 会调整其内容或类似内容的大小。如果对此有更简单的解释和更简单的方法,我仍在等待其他答案。非常感谢您的帮助!

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

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