2009-08-30 7 views
14

내부 문자열의 길이에 따라 높이를 변경해야하는 일부 UITableViewCell이 있습니다. tableView:cellForRowAtIndexPath: 안에 필요한 높이를 계산하고 변수 (self.specialRowHeight)에 저장하고 있습니다. 그럼 내가있어 : 그 제외tableView 가져 오기 : heightForRowAtIndexPath : tableView 다음에 일어날 : cellForRowAtIndexPath :?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == SPECIAL_SECTION) { 
     return self.specialRowHeight; 
    } 
    else { 
     return 44; 
    } 
} 

tableView:cellForRowAtIndexPath: 비트 전에 호출 받고있는 것 같다, 그래서 항상 0입니다.

이 문제를 해결하거나 다른 방법이있을 수 있습니까?

감사합니다.

답변

11

곧 여기에 내 자신의 질문에 대답 :

원래 내가 높이 계산이 tableView:cellForRowAtIndexPath: 방법에 연결하고, 다른 곳에서 이동 될 수 없음을 확신했다. 구조 조정의 무리와 함께, 나는 그 물건을 거기에서 꺼내어 tableView:heightForRowAtIndexPath:에 넣을 수 있었다. 그것은 모든 것을 해결한다. ,

// Inside tableView:cellForRowAtIndexPath: 
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
cell.textLabel.numberOfLines = self.numberOfTextRows; 
// numberOfTextRows is an integer, declared in the class 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    CGSize theSize = [theObject.theStringToDisplay sizeWithFont:[UIFont systemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap]; 
    // This gets the size of the rectangle needed to draw a multi-line string 
    self.numberOfTextRows = round(theSize.height/18); 
    // 18 is the size of the font used in the text label 
    // This will give us the number of lines in the multi-line string 

    if ((indexPath.section == FIXED_HEIGHT_SECTION) || (self.numberOfTextRows < 2)) { 
     return 44; 
     // 44 is the default row height; use it for empty or one-line cells (or in other table sections) 
    } else { 
     return theSize.height + 16; 
     // 16 seems to provide a decent space above/below; tweak to taste 
    } 
} 

당신이 적절한 셀 높이를 계산하는 더 정확한 방법을 생각할 수있는 경우 : 자동 높이 조정 세포가 작동하도록하려고 다른 사람들을위한

, 여기에 도움이 될 몇 가지 코드입니다 나는 모든 귀이다. :)

+0

을 다음과 같은 tableView:heightForRowAtIndexPath:의 구현 내부의 tableView:cellForRowAtIndexPath: 전화를했다 직각 높이를 18로 나누고 다시 곱하면됩니다. 그러나 그것보다 거의 똑같습니다. –

+0

글쎄, 첫 번째 부분은 얼마나 많은 행이 필요한지 알아내는 것입니다. 그래서'cell.textLabel.numberOfLines'을 올바르게 설정할 수 있습니다; 설정하지 않으면 모든 것이 한 줄에 인쇄됩니다. 하지만 네,'self.numberOfTextRows' 대신 마지막 비트에서'theSize.height'를 사용할 수있을 것 같습니다. – Triz

+0

명확성과 단순화를 위해 대답을 편집했습니다. 정말 잘 작동합니다. – Triz

0

self.numberOfLines을 0으로 설정하면 동일하게 작동합니다. (numberOfLines을 0으로 설정하면 필요할 때 다음 라벨은 많은 행을 사용합니다.)

+0

음 ... numberOfTextRows는 사용자 정의 클래스 속성이므로, 내가 말한 것 외에는 아무 것도하지 않습니다. cell.textLabel.numberOfLines을 생각 하시나요? – Triz

+0

어쨌든, 예,'cell.textLabel.numberOfLines'을'0'으로 설정할 수 있습니다 만, 실제로 사용되는 실제 번호를 찾는 것은 여전히 ​​유용합니다 (예를 들어 여기에서와 같이 적절한 행 높이를 계산할 때) . 또한 "0"은 의미 상으로 한눈에 도움이되지 않으므로 훨씬 더 명확하게 결과 코드를 찾습니다. – Triz

-7

내 솔루션은 내가 정말 왜 확실하지 않다

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    [self tableView:aTableView cellForRowAtIndexPath:indexPath]; 
    return headerHeight; 
} 
+3

tableView : heightForRowAtIndexPath :'는 tableView의 모든 (!) 단일 행에 대해 호출됩니다. 이 때문에'tableView : cellForRowAtIndexPath :'와 같은 값 비싼 호출을 사용하지 않아야한다. –

+0

레이블이 유일한 콘텐츠가 아니더라도이 방법을 사용하여 셀의 실제 높이를 테스트 할 수 있습니다. 그러나 이것은 매우 비쌉니다. – Kyle

관련 문제