2016-09-02 2 views
0

기본적으로 10 개의 레이블이있는 셀이 있으며 현재 300 개의 높이로 고정되어 있습니다. 그러나 레이블에 값이 있거나 값이 없거나 레이블에 특정 값이없는 경우이를 숨길 필요가 있습니다. 각 라벨마다 널 (null) 조건을 확인한 다음 높이를 조정하는 것은 길고 지저분합니다. 내가 찾는 것을 성취 할 수있는 다른 방법이 있습니까? enter image description here콘텐츠에 맞게 사용자 지정 UITableViewCell의 크기를 조정하는 방법은 무엇입니까?

+1

사용 자동 레이아웃은 테이블 뷰 셀에 레이블을 모두 제한 할 수 있습니다. 그런 다음 테이블 뷰 델리게이트에서'heightForRowAt'와'estimatedHeightForRowAt'에 대해'UITableViewAutomaticDimension'을 리턴하십시오. 이러한 것들을 제자리에두면 테이블/셀이 크기 조정을 처리합니다. – keithbhunter

+0

모든 라벨을 tableviewcell에 제약 ?? 이걸 좀 더 자세히 설명해 주시겠습니까? –

+0

autolayout 사용법을 읽어보십시오. 그것은 당신이 필요로하는 것입니다. [Apple 웹 사이트의 기사] (https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/) 및 [Ray Wenderlich 튜토리얼] (https://www.raywenderlich.com/115440/) 자동 레이아웃 - 튜토리얼 - in-ios-9-part-1-getting-started-2). – keithbhunter

답변

1

이것은 내가 제안하는 아이디어의 예입니다. 이것이 귀하를 위해 최적화 된 제품을 찾는 데 도움이되기를 바랍니다. 나는 가정 할 경우

, 데이터 소스는 다음과 같이이다 :

// array of arrays of strings 
var Array : [[String]] = [ 
    ["Label1", "Label2", "Label3", "Label4"], 
    ["Label1", "Label2"], 
    ["Label1", "Label2", "Label3", "Label4", "Label5", "Label6"], 
] 

이러한 방법이 구현 될 수있다 :

func calculateExpectedHeight(withItemsCount: Int) -> CGFloat 
{ 
    let titleHeight = 32.0 // this is e.g. your Layer Conf. label 
    let itemHeight = 18.0 // this is for a label unit that is repeating 

    return CGFloat(titleHeight) + CGFloat((Double(withItemsCount) * itemHeight)) + 12.0 //twelve is just for tuning 
} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    let dataForThisRow = Array[indexPath.row] 
    return calculateExpectedHeight(withItemsCount: dataForThisRow.count) 
} 
관련 문제