OStack程序员社区-中国程序员成长平台

标题: ios - Objective C Table Cell ContentView View 层次结构没有为约束准备 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:45
标题: ios - Objective C Table Cell ContentView View 层次结构没有为约束准备

Storyboard

我添加了一个带有 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 *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;
}



Best Answer-推荐答案


约束必须包含第一个布局项清楚地表明您在此调用中缺少 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 层次结构没有为约束准备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37882354/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4