繁体   English   中英

目标C表单元格ContentView没有为约束准备视图层次结构

[英]Objective C Table Cell ContentView The view hierarchy is not prepared for the constraint

故事板

我添加了一个带有故事板的标签(约束顶部: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;
}

约束必须包含第一个布局项目 ,该项目清楚地表明您在此调用中缺少firstItem

NSLayoutConstraint *TComp = [NSLayoutConstraint constraintWithItem:cell.TLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:images attribute:NSLayoutAttributeTop multiplier:1.0 constant:50.0];

也许您需要检查cell.TLabel是否正确加载。 缺少IBOutlet吗?

查看以下导致应用崩溃的原因。

1)如果要使用属性来坚持自己的观点,请确保它们strong 这可以解释他们nil

2)将约束代码从initWithNib方法移至viewDidLoad

3)传递给[NSLayoutContraint constraintWithItem ...]方法的视图为[NSLayoutContraint constraintWithItem ...] ...这也是崩溃的原因

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM