我在表中添加了一个带有 Storyboard的标签(约束顶部:10 左:0 右:0)一个标签。根据状态,如果想创建图像并添加约束,我会收到此错误:
2016-06-17 16:02:59.235 Cellin[3748:162565] ***
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraintconstraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Constraint must contain a first layout item'
解决了缺少 IBOUTLET 的错误; 新错误:
The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fce22494cf0 V:[UILabel:0x7fce22490340'Hello']-(50)-[UIImageView:0x7fce226636c0]>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2016-06-17 16:17:14.152 Cellin[3889:170014] *** Assertion failure in -[UITableViewCellContentView
我的表代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TableCellController *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.TLabel.text = [arr objectAtIndex:indexPath.row];
if(true){
UIImageView *images = [[UIImageView alloc] init];
[images setTranslatesAutoresizingMaskIntoConstraints:NO];
[cell.contentView addSubview:images];
images.image = [UIImage imageNamed:@"image.png"];
NSLayoutConstraint *TComp = [NSLayoutConstraint constraintWithItem:cell.TLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:images attribute:NSLayoutAttributeTop multiplier:1.0 constant:50.0];
[cell.contentView addConstraint:TComp];
}
return cell;
}
最佳答案
Constraint must contain a first layout item 清楚地表明您在此调用中缺少 firstItem。
NSLayoutConstraint *TComp = [NSLayoutConstraint constraintWithItem:cell.TLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:images attribute:NSLayoutAttributeTop multiplier:1.0 constant:50.0];
也许您需要检查 cell.TLabel 是否已正确加载。缺少 IBOutlet 吗?
关于ios - Objective C Table Cell ContentView view hierarchy is not prepared for the constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37882354/