【发布时间】:2017-12-13 14:45:13
【问题描述】:
我正在尝试在我的每个自定义单元格(继承自 UITableViewCell)中设置 NSLayoutConstraint,而不是在 UIViewController 中指定它们,以避免通过公开每种单元格类型的约束来使其混乱。
问题是我找不到单元格生命周期的适当部分来创建布局约束(我尝试了layoutSubviews() 和awakeFromNib()),但是它们都在单元格的生命周期中被调用得太早,当它的@ 987654327@ 尚未定义 - 它会导致此错误:
约束必须包含第一个布局项
既然UITableViewCell 没有viewDidLoad() 像UIViewController 这样的方法,那么如何定义单元格内的布局约束?
layoutSubviews()的自定义UITableViewCell:
- (void) layoutSubviews {
self.translatesAutoresizingMaskIntoConstraints = false;
[[NSLayoutConstraint constraintWithItem: self.attributeNameLabel
attribute: NSLayoutAttributeLeading
relatedBy: NSLayoutRelationEqual
toItem: self.contentView
attribute: NSLayoutAttributeLeading
multiplier: 1
constant: 3] setActive:true];
[[NSLayoutConstraint constraintWithItem: self.attributeNameLabel
attribute: NSLayoutAttributeTop
relatedBy: NSLayoutRelationEqual
toItem: self.contentView
attribute: NSLayoutAttributeTop
multiplier: 1
constant: 3] setActive:true];
[[NSLayoutConstraint constraintWithItem: self.attributeNameLabel
attribute: NSLayoutAttributeRight
relatedBy: NSLayoutRelationEqual
toItem: self.contentView
attribute: NSLayoutAttributeRight
multiplier: 1
constant: 3] setActive:true];
[[NSLayoutConstraint constraintWithItem: self.attributeNameLabel
attribute: NSLayoutAttributeBottom
relatedBy: NSLayoutRelationEqual
toItem: self.contentView
attribute: NSLayoutAttributeBottom
multiplier: 1
constant: 3] setActive:true];
}
UIViewController 中的示例初始化:
- (UITableViewCell *)tableView:(UITableView *)view cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.row % 2 == 0) {
cell = [[CustomCell1 alloc] init];
} else if (indexPath.row % 2 == 1) {
cell = [[CustomCell2 alloc] init];
} else if (...) {
...
}
return cell;
}
【问题讨论】:
-
你没有使用xib或storyboard?
-
我不想使用故事板,因为我有许多不同的单元格类型(具有继承等)......它很快就会变得混乱
-
你有没有在设置约束后尝试过updateConstraintsIfNeeded()?
-
不,我不能,应用程序甚至没有设置第一个约束。它在这一行崩溃:
[[NSLayoutConstraint constraintWithItem:... -
尝试使用锚点。它更清洁。见here
标签: ios objective-c uitableview nslayoutconstraint