2016-09-02 3 views
0

스토리 보드가없는 이전 Objective-C 프로젝트로 작업하고 있습니다. 3 개의 레이블이 포함 된 UITableViewCell으로 결과를 표시하려면 UITableView이 필요합니다. 작업은 여기에 UITableviewCell목표 C 코드의 자동 행 높이 조절

에 수직으로 3 개 레이블을 정렬하는 것입니다 내 라벨 개시 :

serviceTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 200, LONG_MAX)]; 
[serviceTitleLabel setFont:[UIFont SingPostRegularFontOfSize:16.0f fontKey:kSingPostFontOpenSans]]; 
[serviceTitleLabel setNumberOfLines:0]; 
[serviceTitleLabel setTextColor:RGB(51, 51, 51)]; 
[serviceTitleLabel setBackgroundColor:[UIColor clearColor]]; 
[serviceTitleLabel setTextAlignment:NSTextAlignmentLeft]; 
[contentView addSubview:serviceTitleLabel]; 

statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 30, 200, 30)]; 
[statusLabel setFont:[UIFont SingPostBoldFontOfSize:12.0f fontKey:kSingPostFontOpenSans]]; 
[statusLabel setTextColor:RGB(125, 136, 149)]; 
[statusLabel setBackgroundColor:[UIColor clearColor]]; 
[contentView addSubview:statusLabel]; 

costLabel = [[UILabel alloc] initWithFrame:CGRectMake(216, 20, 85, 30)]; 
costLabel.right = contentView.right - 15; 
[costLabel setFont:[UIFont SingPostBoldFontOfSize:16.0f fontKey:kSingPostFontOpenSans]]; 
[costLabel setTextColor:RGB(51, 51, 51)]; 
[costLabel setTextAlignment:NSTextAlignmentRight]; 
[costLabel setBackgroundColor:[UIColor clearColor]]; 
[contentView addSubview:costLabel]; 

크기가 라벨의 크기에 따라 크기를 조정할 수 있도록 우리가 어떻게 UITableViewController에서 조정할 수 있습니다? 셀과 테이블 뷰 모두에서 설정해야합니까? 어떤 도움이라도 대단히 감사합니다. 감사.

지금이 화면입니다. 하지 못

enter image description here

+0

를 추가 – ddb

+0

안녕은 동적있는 tableview가의 데이터가 함께 항목의 배열에서 (3 레이블은 항목 속성입니다) –

+0

각 셀의 맞춤 높이가 있어야하거나 계산 된 높이가 모든 셀에 유효해야합니다. – ddb

답변

2

을 구현 당신은 내용에 따라 셀 높이를 관리 할 수 ​​UITableViewAutomaticDimension을 사용해야합니다. 즉, PIN을 클릭 한 다음 Constraint from margin 확인란을 선택 취소하고 두 레이블 모두 위쪽, 아래쪽, 왼쪽 및 오른쪽에서 제약 조건을 추가합니다. 필요에 따라 UITableViewAutomaticDimension을 사용하려는 경우 당신은 하나의 셀 또는 하나 개 이상의 셀이 하나 개있는 tableView이있는 경우

지금 viewDidLoad 방법에 ViewController에서 나에게 분명하지 않다,이 두 라인

self.tableView.estimatedRowHeight = 44.0; 
self.tableView.rowHeight = UITableViewAutomaticDimension; 
+0

고마워. 내 문제는 스토리 보드가 없다는 것. 모두 코드에서해야한다. –

1

솔루션은 귀하의 아이폰 OS 전개 대상에 따라 달라 봐. iOS 8 이상인 경우 :

self.tableView.rowHeight = UITableViewAutomaticDimension; 

이 솔루션은 셀의 제약 조건이 올바르게 설정된 경우 작동합니다. 당신이 스스로를 계산해야보다 낮은 있다면

, UITableViewDelegate

- tableView:heightForRowAtIndexPath: 
+0

안녕하세요. 덕분에 도움을 주셔서 감사합니다. 어디에 줄을 써야하나요? self.tableView.rowHeight = UITableViewAutomaticDimension ;? 어쨌든 셀에 대한 제약을 설정합니다. 셀에 3 개의 레이블 만 있음 –

+0

tableview 컨트롤러에서 self.tableView.rowHeight로 설정합니다 (예 : viewDidLoad). 제약 조건 : 모든 레이블에 제약 조건을 설정해야합니다. 모든 테이블에는 모든면 (위쪽, 오른쪽, 아래쪽, 왼쪽)에 대한 제약 조건이 있어야합니다. – Konstantin

+0

그래서 코드의 3 개 레이블 모두에 대해 포함해야합니까? 4 1 라벨. 12 레이블에 3 개가 들어 있습니까? 나는 스택 뷰 방식을 시도하고있다. 희망은 간단 할 수있다 –