繁体   English   中英

UITableViewCell中的NSLayoutConstraint中断

[英]NSLayoutConstraint break within UITableViewCell

当使用自动布局约束以编程方式将viewController.view添加到表视图单元时,有时无法正确布局。 知道单元格具有固定的高度(50),并且子视图包含一个UIButton和一个UITableView ,在用户开始执行某些操作之前,它们不包含单元格。
这是将子视图添加到单元格的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //Get sub item objects
    BookItem * newItem = self.subItems[indexPath.row];
    NSString * newItemKey = [NSString stringWithFormat:@"%i", newItem.iD];
    NSString * currentCellItemKey = [NSString stringWithFormat:@"%i", cell.tag];
    BookTreeItemViewController_iPad * bookTreeItemVC = self.subItemVCs[currentCellItemKey];
    if (!bookTreeItemVC)
    {
        bookTreeItemVC = [self.storyboard instantiateViewControllerWithIdentifier:@"BookTreeItemViewController_iPad"];
        //Clear cell
        for (UIView * subView in cell.contentView.subviews)
            [subView removeFromSuperview];
        //Add sub item view to cell
        [bookTreeItemVC.view setTranslatesAutoresizingMaskIntoConstraints:NO];
        [cell.contentView addSubview:bookTreeItemVC.view];
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[item]|" options:nil metrics:nil views:@{@"item": bookTreeItemVC.view}]];
        [cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[item]|" options:nil metrics:nil views:@{@"item": bookTreeItemVC.view}]];
    }
    bookTreeItemVC.bookItem = newItem;

    //Replace the old book tree item view controller with the new one
    [self.subItemVCs setValue:nil forKey:currentCellItemKey];
    cell.tag = newItem.iD;
    self.subItemVCs[newItemKey] = bookTreeItemVC;

    return cell;
}

这是约束警告:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7c091240 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7c0895d0(49.5)]>",
    "<NSLayoutConstraint:0x7a74f250 V:[UIButton:0x7a7482a0'Title']-(8)-[UITableView:0x7aad7a00]>",
    "<NSLayoutConstraint:0x7a74f2b0 V:[_UILayoutGuide:0x7a74ee70]-(0)-[UIButton:0x7a7482a0'Title']>",
    "<NSLayoutConstraint:0x7a74f310 V:[UITableView:0x7aad7a00]-(0)-[_UILayoutGuide:0x7a74ef60]>",
    "<_UILayoutSupportConstraint:0x7a739510 V:[_UILayoutGuide:0x7a74ee70(87)]>",
    "<_UILayoutSupportConstraint:0x7a74e2e0 V:|-(0)-[_UILayoutGuide:0x7a74ee70]   (Names: '|':UIView:0x7a74ee00 )>",
    "<_UILayoutSupportConstraint:0x7a747900 V:[_UILayoutGuide:0x7a74ef60(0)]>",
    "<_UILayoutSupportConstraint:0x7a748130 _UILayoutGuide:0x7a74ef60.bottom == UIView:0x7a74ee00.bottom>",
    "<NSLayoutConstraint:0x7a750c10 V:|-(0)-[UIView:0x7a74ee00]   (Names: '|':UITableViewCellContentView:0x7c0895d0 )>",
    "<NSLayoutConstraint:0x7a748270 V:[UIView:0x7a74ee00]-(0)-|   (Names: '|':UITableViewCellContentView:0x7c0895d0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7a74f250 V:[UIButton:0x7a7482a0'Title']-(8)-[UITableView:0x7aad7a00]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

顺便说一句,我从警告中注意到,顶部布局指南的高度为(87),应该为(0)

是的,你是对的。

"<_UILayoutSupportConstraint:0x7a739510 V:[_UILayoutGuide:0x7a74ee70(87)]>",

不知何故,您的视图控制器的布局指南的最高支持值为87。由于固定单元格高度而导致崩溃,因此它消除了按钮与表格视图之间的距离限制。

也许您可以发布BookTreeItemViewController_iPad的布局代码,或者

  • 尝试从新的xcode进行3D调试,找出那87像素是什么?
  • 将您的TableView.rowHeight设置为UITableViewAutomaticDimension,以便您的单元格高度将是动态的。 然后,您可以再次看到87像素是多少。

该问题看起来像在删除视图时,约束没有被删除。 因此,只需替换以下代码:

更换:-

for (UIView * subView in cell.contentView.subviews)
 [subView removeFromSuperview];

至:-

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];     

并且还以更有效的方式修改了以下内容:

NSView *views=bookTreeItemVC.view;
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[item]|" options:0l metrics:nil views:NSDictionaryOfVariableBindings(views)]];
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[item]|" options:0l metrics:nil views:NSDictionaryOfVariableBindings(views)]];

经过两天的研究,我不得不从IB(故事板)中删除子视图的表格视图(放入单元格的视图),并以编程方式添加其约束。 因此,现在一切正常。

暂无
暂无

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

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