2014-02-08 2 views
0

현재 UITableViewCell에서 UILabel 높이를 조정하고 있습니다. 이제 클라이언트는 UITableViewCell에 포함 된 두 레이블을 내용에 따라 크기를 조정하려고합니다. 이를 연구하면서 단일 레이블의 크기를 조정할 수있는 솔루션을 어디에서나 볼 수 있지만 다중 레이블은 필요하지 않습니다. 누군가이 문제에 대한 좋은 자료를 알려줄 수 있습니까?UITableViewCell 여러 개의 UILabels 자동 크기 조정

+0

autolayout을 사용합니까? 대상 ios 버전은 무엇입니까? – vokilam

답변

0

레이블 크기 조정에 대한 연구가 정확하면 하나의 레이블에만 크기 조정 논리를 작성해야합니다. 보시다시피

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

반환 유형 uitableviewcell. 각 행에 대해 정적 셀을 작성하지 않는 한 항상 셀에 대해 동일한 구성을 리턴합니다.

[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

이 메서드는 셀을 dequeue & 다시 사용합니다. 셀을 만들 때 레이블의 크기를 조정하는 논리를 올바르게 작성해야합니다. 또는 사용자 정의 셀 클래스의 크기 조정 논리를 올바르게 수정하는 것이 좋습니다. 나는이 문제에 도움이되는 코드를 추가하고있다.

CGSize maximumLabelSize = CGSizeMake(270, FLT_MAX); 
CGSize expectedLabelSize = [myString sizeWithFont:[UIFont fontWithName:@"Helvetica" size:16.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping]; 

NSLog(@"Expected Size of label based on string--%@",NSStringFromCGSize(expectedLabelSize)); 
//adjust the label the the new height. 
CGRect newFrame = myLabel.frame;//get initial frame of your label 
newFrame.size.height = expectedLabelSize.height; 
myLabel.frame = newFrame; 
NSLog(@"new frame of label--%@",NSStringFromCGRect(newFrame)); 
[myLabel setText:attString];