2014-01-18 2 views
5

서브 클래 싱 된 UITableViewCell로 작업 중이며 자동 레이아웃을 사용하는 동안 UILabel의 텍스트를 모두 왼쪽 위 정렬해야합니다. 그 sizeToFit 실제로 자동 레이아웃 함께 사용해서는 안 읽고 그것을 피하고 싶습니다, 그리고 어떻게 든 제약 조건을 사용하고 싶습니다. 기본적으로 레이블의 텍스트는 셀을 재사용 할 때마다 다시 설정되므로 크기 조정은 재사용시 동적이어야합니다.UITableViewCell 셀 재사용 및 UILabel 자동 레이아웃 동적 크기 조정

- (UILabel *)commentsLabel { 

    if (_commentsLabel == nil) { 

     _commentsLabel = [[UILabel alloc] init]; 
     [_commentsLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; 
     _commentsLabel.numberOfLines = 0; 
     _commentsLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     _commentsLabel.preferredMaxLayoutWidth = 100; 
    } 

    return _commentsLabel; 
} 

라벨에 설정되는 자동 레이아웃 제약이 있습니다 (commentsLabel은 서브 뷰 셀의 서브 클래스에 self.customView에 추가됩니다) :

여기에 서브 클래 싱 된 세포 내부의 게으른 초기화 라벨입니다

[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-locationToTopPadding-[locationLabel(locationHeight)]-locationToCommentsPadding-[commentsLabel]-commentsToBottomPadding-|" 
                     options:0 
                     metrics:@{ 
                        @"locationToTopPadding":@(locationToTopPadding), 
                        @"locationHeight":@(locationHeight), 
                        @"locationToCommentsPadding":@(locationToCommentsPadding), 
                        @"commentsToBottomPadding":@(commentsToBottomPadding) 
                        } 
                      views:viewsDictionary]]; 

[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[thumbnailImageView]-[commentsLabel]-|" 
                     options:0 
                     metrics:@{@"commentsWidth":@(commentsWidth)} 
                      views:viewsDictionary]]; 

단지 설정 :

self.commentsLabel.preferredMaxLayoutWidth = 100; 

이 althoug를 작동하지 않는 것 h는 대부분의 답에서 언급됩니다.

에는 현재 구현되었지만 결과가 표시되지 않습니다. UILabel sizeToFit doesn't work with autolayout ios6

다른 답변을 시도했습니다. UILabel sizeToFit only works with AutoLayout turned off

는 난 그냥 위의 제약 그러나 나는 일을하거나 예외를 발생하지 않는 추가하려고 프로그래밍 제약 이외에 추가 할 수 있습니다 (1 개) 제약 조건을 누락 생각합니다. 코드에서 완전히 xibs가 없습니다.

ETA : 기존의 수직 구속 라인 내에서 높이 제약 조건을 설정했습니다 : 같은

여기 제안 : Dynamically change UILabel width not work with autolayout

그것을 만드는 위의 수직 구속 라인 :

-[commentsLabel(>[email protected])]- 

내가했습니다 높이 값과 우선 순위 값으로 어지럽 혀 아무것도 바뀌지 않았습니다.

ETA : 이벤트에

->=commentsToBottomPadding- 
+0

이 게시물을 본 적이 있습니까? http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – smileyborg

답변

1

솔루션 : 일부 진전, 나는 몇 가지가 아니다 올바르게 정렬되어있는 라벨의 일부를 시도, 그것은 라벨의 하단에 패딩과 관련이있다 생각

-[commentsLabel]->=yourBottomPadding- 

가에서 그 수직 제약에

호출이를 넣어 : 다른 하나는있는 UITableViewCell 동적으로 변경하는 사용자 지정 라벨과 같은 문제로 실행 당신이 텍스트를 설정 한 후 viewController :

[cell updateConstraints]; 
관련 문제