2014-04-23 2 views
1

본질적으로 보통 textLabel 레이블을 대체하는 사용자 지정 레이블을 가진 UITableViewSubclass (스타일 : UITableViewCellStyleValue1)가 있습니다. 이 값은 보통 textLabel으로 정렬해야하지만 너비는 보통 detailTextLabel의 왼쪽에 8 포인트 끝나야합니다. 원하는 효과를 얻지 만 셀을 사용하면 동시에 제약 조건을 충족시킬 수 없다는 예외가 발생합니다.UITableViewCell에 자동 레이아웃 제약 조건 충돌이 발생하는 이유

내가 이해할 수없는 것은 그것이 불평하는 이유와 명백한 충돌이 없다는 것입니다. 제약 조건을 설정 한 후 [self layoutIfNeeded]를 호출하면 예외가 사라집니다. 또는 후행 속성 (예 : UILayoutPriorityRequired 대신 UILayoutPriorityDefaultHigh 대신)을 구성하는 제약 조건의 우선 순위를 낮추면 자동 레이아웃 엔진에 해당 제약 조건을 무시/무시할 수 있음을 알리고 있기 때문에 예외가 사라지게됩니다.

표준 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. 
+0

나는 같은 간단한 문제가 이러한 두통을 유발할 수 있다는 사실 때문에 나를 iOS SDK에 아프게합니다. – dreamzor

답변

1
당신은 혼합하지해야

textLabeldetailTextLabel를 사용하여 일치합니다. 크기가 내부적으로 관리되고 예기치 않은 결과가 발생할 수 있기 때문에 이러한 제약을받을 수 없습니다.

고유 한 레이블을 만든 다음 제한을 설정해야합니다. UITableViewCell에 대한 문서에서

: 세포를 만들 때

, 당신은 그들에게 자신을 정의하거나 미리 정의 된 여러 스타일 중 하나를 사용할 수 있습니다. 미리 정의 된 셀 스타일은 가장 간단한 옵션입니다. 미리 정의 된 스타일을 사용하여 셀은 위치와 스타일이 고정 된 레이블 및 이미지 하위 뷰를 제공합니다. 고정보기에 들어가기 위해 텍스트 및 이미지 컨텐츠를 제공하기 만하면됩니다. 사전 정의 된 스타일로 셀을 사용하려면 initWithStyle:reuseIdentifier: 메소드를 사용하여 셀을 초기화하거나 Xcode에서 해당 스타일로 셀을 구성하십시오. 셀의 텍스트와 이미지를 설정하려면 textLabel, detailTextLabelimageView 속성을 사용하십시오.

미리 정의 된 스타일을 벗어나고 싶다면 셀의 contentView 속성에 하위보기를 추가 할 수 있습니다. 하위보기를 추가 할 때보기를 배치하고 자신의 콘텐츠를 설정해야합니다.

관련 문제