2013-09-29 1 views
0

에 "\ n을"문자가있는 경우 UITextView 내용 크기의 높이를 계산. \ n을이 여전히 텍스트 뷰 문자열이 새 라인 문자가 포함 된 경우를 제외하고, 아이폰 OS 7에 나를 위해 노력하고 있습니다내용이 내가 UITextViews를 포함하는 사용자 지정 UITableViewCells이 아이폰 OS 7

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Get the appropriate content string 
    NSString *sectionKey = [self.orderedSectionKeys objectAtIndex:indexPath.section]; 
    NSDictionary *infoDictionary = [[self.tableViewData objectForKey:sectionKey] objectAtIndex:indexPath.row]; 
    NSString *textViewString = [infoDictionary objectForKey:@"description"]; 

    // Calculate the UITextView height 
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, kInfoDefaultTableViewCellTextViewWidth, kInfoDefaultTableViewCellTextViewHeight)];   
    textView.font = [UIFont fontWithName:kInfoDefaultTableViewCellTextViewFont 
            size:kInfoDefaultTableViewCellTextViewFontSize]; 
    textView.text = textViewString; 
    [textView layoutSubviews]; // Call this so that the content size is set 

    CGFloat height = textView.contentSize.height;   
    height += kInfoDefaultTableViewCellHeightBuffer; 

    return height; 
} 

: 아이폰 OS의 이전 버전에서는 동적으로이 같은 테이블 뷰 셀 크기. 그런 다음 contentSize.height가 너무 작습니다. 새 줄을 추가하지 않고 \ n을 문자로 처리합니다. 실제 테이블 뷰에는 새 행이 추가됩니다.

는 아무도 내가 새로운 라인으로 적절한 높이를 얻을 수있는 방법을 알고 있나요? 이 솔루션에서 기술을 시도했습니다 : https://stackoverflow.com/a/18818036/472344,하지만 게시 된 코드에서 사용했던 것과 같은 결과가 나타납니다.

+0

해당 문자열에서 @ "\ n"을 찾지 말고 필요에 따라 일정한 높이 또는 무언가를 곱하십시오. ..! – Bala

+0

http://stackoverflow.com/a/6111719/1059705 – Bala

+0

고마워 ... 나는 내 코드로 놀아 왔고, 내가 일을하기 위해 연결 한 대답에 해결책을 얻었다. 왜 그것이 전에 작동하지 않았는지 모르겠습니다. 당신의 솔루션이 잘 작동한다고 상상해보십시오.하지만 구현하는 데에는 약간의 어려움이 있습니다. – Darren

답변

2

contentView의 textView에 KVO를 추가합니다. 그런 다음 변경 사항을 얻은 경우 행을 다시로드하십시오.

[textView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil]; 

    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    // Reload your exact row here 
    } 
+0

작업이 끝나면 Observer를 제거하는 것을 잊지 마십시오. – capikaw

관련 문제