2014-11-06 5 views
0
내가있는 UITableViewCell 레이아웃 다음, 나는이 여기

있는 UITableViewCell 높이 자동 레이아웃

========= 
| Image | Name Label 
|  | Short description Label 
========= 
Description Label 

설명 라벨는 선택 사항입니다 처리하기위한 자동 레이아웃 사용하고있다

, 그 내용에 따라/쇼를 숨 깁니다, 내가 사용 heightForRowAtIndexPath에 셀의 높이를 계산하고 내가 숨기기 설명 라벨이 셀 같은 높이를 반환을 숨길 경우에도

- (CGFloat)heightForTableView:(UITableView *)tableView cell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    cell.bounds = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds)); 

    [cell setNeedsLayout]; 
    [cell layoutIfNeeded]; 

    CGSize cellSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 

    // Add extra padding 
    CGFloat height = cellSize.height;// + 1; 

    return height; 
} 

, 나는 무엇을 놓치고?

누구나 자동 레이아웃으로 이러한 시나리오를 처리하는 가장 좋은 방법을 제안 할 수 있습니까?

편집 1 : 빈 작업을 설정하지만 더 좋은 방법이 있습니까?

+0

셀 높이 계산의 전체 코드를 표시 할 수 있습니까? –

+0

레이블을 숨기고있는 위치는 어디입니까? 이 메소드가 호출되기 전에? –

+0

예 – Abhishek

답변

0

heightForRowAtIndexPath도 모든 행에 대해 호출됩니다. 그래서 동일한 행에 대해 heightForRowAtIndexPath 메소드의 높이를 설정합니다. 설명 레이블을 숨기는 행의 경우.

예를 들면. 행 번호 4에 대한 일부 조건을 검사하여 설명 레이블을 숨기려고합니다.

heightForRowAtIndexPath보다 동일한 행에 대해 동일한 조건을 확인할 수 있으며 설명 레이블을 표시하지 않는 동안 필요한 높이를 반환 할 수 있습니다.

if(description.length==0) 
{ 
    return 100;// description label hidden 
} 
else 
{ 
    return 140;// description label shown 
} 
+0

셀 높이 계산 방법을 업데이트했습니다. 정확한 값을 반환하고 싶습니다. – Abhishek

+0

셀 높이 계산 방법을 잊어 버렸습니다. 나는 일해야만한다 –

+0

나는 다른 세포가 너무 많아서 단순히 사용하지 않는다. – Abhishek

0

나는 다음과 같은 각 셀의 높이를 계산하는 제안 말할 수 있습니다. 귀하의 질문을 검토함으로써, 설명 레이블이없는 경우 모든 세포가 동일한 높이를 가져야한다고 가정합니다. 60이라고 가정합니다. 따라서 설명 텍스트에 따라 각 셀의 높이를 계산하고 설명 텍스트가없는 셀 높이에이를 추가하면됩니다. 귀하의 heightForTableView 대리인은 다음과 같습니다. 내가 calculateHeightForText 위해 두 가지 방법을 쓴

- (CGFloat)heightForTableView:(UITableView *)tableView cell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    int cellWidth = 320; // assuming it is 320 
    int constantHeight = 60; 

    NSString * str = [[yourarray objectAtIndex:indexPath.row] objectForKey:@"DescriptionKey"]; 

    int height = [self calculateHeightForText:str withWidth:cellWidth andFont:[UIFont systemFontOfSize:16]]; 
    // replace systemFontSize with your own font 

    return height + constantHeight; 
} 
// Depreciated in iOS7 
- (int)calculateHeightForText:(NSString *)string withWidth:(int)width andFont:(UIFont *)font 
{ 
    CGSize textSize = [string sizeWithFont:font constrainedToSize:CGSizeMake(width, 20000) lineBreakMode: NSLineBreakByWordWrapping]; 

    return ceil(textSize.height); 
} 
// Introduced in iOS7 
- (int)calculateHeightForText:(NSString *)string withWidth:(int)width andFont:(UIFont *)font 
{ 

    int maxHeightForDescr = 1000; 
    NSDictionary *attributes = @{NSFontAttributeName: font}; 

    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, maxHeightForDescr) 
             options:NSStringDrawingUsesLineFragmentOrigin 
            attributes:attributes 
             context:nil]; 
    return rect.size.height; 
} 

, 하나는 iOS7에에서 감가 상각 및 iOS6의 적은 및 iOS7에 있지만, 2 일 모두 작동됩니다 iOS7에 대한 방법을 권장하지만, iOS7에 대해 작동하지 않습니다. 혼란 스럽거나 더 많은 도움이 필요하면 알려주십시오. 더 멀리 도울 수있어서 기뻐할 것입니다.