为什么UITableViewCell具有自动布局约束冲突

时间:2014-04-23 18:54:17

标签: ios objective-c cocoa-touch uitableview autolayout

我有一个UITableViewSubclass(样式:UITableViewCellStyleValue1),它有一个自定义标签,基本上是普通textLabel标签的替代品。它应该与法线textLabel保持对齐,但宽度要比正常detailTextLabel的左边8点。我得到了预期的效果,但是当使用单元格时,会抛出一个异常,即无法同时满足约束条件。

我不明白为什么它抱怨,没有明显的冲突。在设置约束后调用[self layoutIfNeeded]会使异常无效。或者,降低配置尾随属性的约束的优先级(例如,UILayoutPriorityDefaultHigh而不是默认的UILayoutPriorityRequired)也会使异常静音,显然是因为我告知Auto Layout引擎打破/忽略这种约束是可以的。

我假设标准的UITableView实现可能奇怪地以一种方式布局textLabeltextLabel,这使得它最初无法描述所描述的视图。例如,如果textLabel实际放置在单元格的右侧并且detailTextLabel放置在左侧,那么我所描述的布局将是不可能的。

在此方案的上下文中,自动布局何时生效。它是否正在跳枪并试图在它应该之前布置东西?

没有使用故事板或XIB。纯粹以代码为基础。

#import "EBTStoreTableViewCell.h"

@interface EBTStoreTableViewCell ()
@property (nonatomic, readonly, weak) UILabel *storeNameLabel;
@end

@implementation EBTStoreTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textLabel.text = @" ";

        self.detailTextLabel.text = @" ";

        UILabel *storeNameLabel = [[UILabel alloc] init];
        storeNameLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [storeNameLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
        [self.contentView addSubview:storeNameLabel];
        _storeNameLabel = storeNameLabel;

        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:storeNameLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.textLabel attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:storeNameLabel attribute:NSLayoutAttributeBaseline relatedBy:NSLayoutRelationEqual toItem:self.textLabel attribute:NSLayoutAttributeBaseline multiplier:1.0f constant:0.0f]];

        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:storeNameLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.detailTextLabel attribute:NSLayoutAttributeLeading multiplier:1.0f constant:-8.0f]];

//        [self layoutIfNeeded]; // doing this makes the issue go away
    }
    return self;
}

// Setters ommited

@end

这是我收到的异常消息:

2014-04-23 11:50:42.092 Application[32507:60b] 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:0xde03910 UILabel:0xde036b0.leading == UILabel:0xde00590.leading>",
    "<NSLayoutConstraint:0xde03a30 UILabel:0xde036b0.trailing == UITableViewLabel:0xde00c10.leading - 8>",
    "<NSAutoresizingMaskLayoutConstraint:0xde01920 h=--& v=--& UILabel:0xde00590.midX ==>",
    "<NSAutoresizingMaskLayoutConstraint:0xde1de50 h=--& v=--& H:[UILabel:0xde00590(0)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x17f50a10 h=--& v=--& UITableViewLabel:0xde00c10.midX ==>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0xde03a30 UILabel:0xde036b0.trailing == UITableViewLabel:0xde00c10.leading - 8>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

1 个答案:

答案 0 :(得分:1)

您不应该使用textLabeldetailTextLabel进行混合和匹配。这些不受限制,因为它们的大小在内部进行管理并可能产生意外结果。

您应该创建自己的标签,然后对它们设置约束。

来自UITableViewCell的文档:

  

创建单元格时,您可以自己自定义单元格,也可以使用多种预定义样式中的一种。预定义的单元格样式是最简单的选项。使用预定义样式,单元格提供其位置和样式固定的标签和图像子视图。您所要做的就是提供文本和图像内容以进入这些固定视图。要使用具有预定义样式的单元格,请使用initWithStyle:reuseIdentifier:方法对其进行初始化,或者在Xcode中使用该样式配置单元格。要设置单元格的文本和图像,请使用textLabeldetailTextLabelimageView属性。

     

如果您想超越预定义的样式,可以将子视图添加到单元格的contentView属性中。添加子视图时,您负责定位这些视图并自行设置其内容。

相关问题