Ios 为什么UITableViewCell存在自动布局约束冲突

Ios 为什么UITableViewCell存在自动布局约束冲突,ios,objective-c,cocoa-touch,uitableview,autolayout,Ios,Objective C,Cocoa Touch,Uitableview,Autolayout,我有一个UITableViewSubclass(样式:UITableViewCellStyleValue1),它有一个自定义标签,基本上是普通textLabel标签的替代品。它应该与正常的textLabel左对齐,但在正常的detailTextLabel左侧有8个端点的宽度。我得到了期望的效果,但是当使用单元格时,会抛出一个异常,表示无法同时满足约束 我不明白的是它为什么抱怨,没有明显的冲突。设置约束后调用[self-layoutIfNeeded]会使异常静音。或者,降低配置尾部属性的约束的优先

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

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

我假设标准UITableView实现可能会以一种最初无法描述所描述视图的方式奇怪地布置
textLabel
textLabel
。例如,如果
textlab
实际放在单元格的右侧,而
detailtextlab
放在左侧,那么我描述的布局就不可能了

在此场景中,“自动布局”何时生效。它是不是抢在前面,试图在预期的时间内布置东西

没有使用故事板或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.
2014-04-23 11:50:42.092应用程序[32507:60b]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在objc_异常_抛出时中断,以便在调试器中捕获该异常。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。

您不应该使用
textlab
detailtextlab
进行混搭。由于它们的大小由内部管理,可能会产生意外的结果,因此无法对其进行约束

您应该创建自己的标签,然后对其进行约束

从以下文件:

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

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


我也有同样的问题,这么简单的事情会让我头疼,这让我厌倦了iOS SDK。UITableView已经不再有用了。我更愿意在所有需要表格的地方使用UICollectionView,因为它得到了苹果支持团队更多的支持,这包括自动布局支持