我正在尝试在通过程序实现的自定义UITableViewCell上使用iOS 6的新自动布局功能。我添加了addConstraint调用,并且一开始它能正常工作——直到我滚动。当我滚动后回来时,布局就被破坏了。被破坏的意思是字段之间的边距都不对了(太大了,超出了单元格的大小)。我猜测这可能与dequeueReusableCell方法有关,会让我得到一个“脏”的单元格,就像你发现自己需要重新初始化单元格内的字段一样,但我似乎无法做任何事情来使它再次正确地呈现。我尝试在返回单元格之前调用[self.contentView updateConstraints]。我尝试销毁约束并重新创建它们。不仅不起作用,而且如果在layoutSubviews中尝试它,它会陷入某种无限循环的冻结状态。有什么想法吗?
以下是建立约束的代码。它位于initWithStyle:reuseIdentifier:中:
以下是建立约束的代码。它位于initWithStyle:reuseIdentifier:中:
[self.completedLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.nextSetHeaderLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.nextSetDetailLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.youWillLearnHeaderLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.youWillLearnDetailLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView removeConstraints:[self.contentView constraints]];
NSDictionary *views = NSDictionaryOfVariableBindings(_completedLabel, _nextSetHeaderLabel, _nextSetDetailLabel, _youWillLearnHeaderLabel, _youWillLearnDetailLabel);
[self.contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_completedLabel]-5-|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_nextSetHeaderLabel]-5-|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_nextSetDetailLabel]-5-|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_youWillLearnHeaderLabel]-5-|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[_youWillLearnDetailLabel]-4-|"
options:0
metrics:nil
views:views]];
[self.contentView addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[_completedLabel]-12-[_nextSetHeaderLabel]-0-[_nextSetDetailLabel]-12-[_youWillLearnHeaderLabel]-0-[_youWillLearnDetailLabel(>=20)]-1-|"
options:0
metrics:nil
views:views]];