gpt4 book ai didi

ios - UITableViewCell 的 NSLayoutConstraint : Constraint must contain a first layout item

转载 作者:行者123 更新时间:2023-11-28 21:02:20 25 4
gpt4 key购买 nike

我正在尝试在我的每个自定义单元格(继承自 UITableViewCell)中设置 NSLayoutConstraint,而不是在 UIViewController 中指定它们以通过公开每种单元格类型的约束来避免困惑。

问题是我找不到单元格生命周期的适当部分来创建布局约束(我尝试了 layoutSubviews()awakeFromNib()),但是它们都在单元格的生命周期中过早地被调用,当它的 superview 还没有被定义时——它会导致这个错误:

Constraint must contain a first layout item

因为 UITableViewCell 没有像 UIViewController 那样的 viewDidLoad() 方法,如何在单元格内定义布局约束?

自定义 UITableViewCell

layoutSubviews():

- (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;
}

最佳答案

尝试制作

 self.attributeNameLabel.translatesAutoresizingMaskIntoConstraints = NO;

对于每个标签而不是 contentView

像对我有用的那样转换它们

这样做

  - (void)awakeFromNib {
// Initialization code



[super awakeFromNib];


UILabel*ddd = [UILabel new];

ddd.text = @"adsbcujklasdcbaksci";

ddd.translatesAutoresizingMaskIntoConstraints = NO ;

[self.contentView addSubview:ddd];

NSLayoutConstraint*ss1 = [NSLayoutConstraint constraintWithItem: ddd
attribute: NSLayoutAttributeCenterX
relatedBy: NSLayoutRelationEqual
toItem: self.contentView
attribute: NSLayoutAttributeCenterX
multiplier: 1 constant: 3] ;
NSLayoutConstraint*ss2 = [NSLayoutConstraint constraintWithItem: ddd
attribute: NSLayoutAttributeCenterY
relatedBy: NSLayoutRelationEqual
toItem: self.contentView
attribute: NSLayoutAttributeCenterY
multiplier: 1 constant: 3] ;

[self.contentView addConstraints:@[ss1,ss2]];



[self layoutSubviews];

[self layoutIfNeeded];



}

关于ios - UITableViewCell 的 NSLayoutConstraint : Constraint must contain a first layout item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47795784/

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