2013-01-18 2 views
1

그래서이 문제와 관련된 많은 것을 보았습니다. 그러나 그 중 아무 것도 내 질문과 관련이 없습니다.UITextView는 UITableViewCell의 내부에서 확장됩니다.

메시징 기능이있는 앱을 만듭니다. 애플이 Mail과 LinkedIn을 가지고있는 것과 비슷하게 애플 리케이션에 Mail 기능이있다. 나는 UITableView을 3 행으로 갖고 싶다. 세 번째 행은 사용자 유형에 따라 증가해야하는 UITextView입니다. 내 코드는 다음과 같습니다.

- (void)textViewDidChange:(UITextView *)textView { 
    if (bodyText.contentSize.height > currentContentHeight) { 
     currentContentHeight = bodyText.contentSize.height; 

     [tblView beginUpdates]; 
     [tblView endUpdates]; 

     [bodyText setFrame:CGRectMake(0, 0, 310.0, currentContentHeight)]; 

     bodyText.selectedRange = NSMakeRange(textView.text.length - 1, 0); 

    } else { 
     currentContentHeight = minimumContentHeight; 

     [tblView beginUpdates]; 
     [tblView endUpdates]; 
    } 
} 

iPhone에서 리턴 키를 누르면 작동이 중단되고 완벽하게 작동합니다. 문제는 내가 센터 또는 UITextView의 다른 중간 부분으로 이동하면 contentSize이 잘못 되었기 때문에 재미있는 행동을하는 것처럼 보입니다. 예를 들어 :

  • 나는 수익의 10 배
  • 고토 다섯 번째 라인과 유형 '개'
  • 그것은 contentHeight 그 5 번째 줄에 기반하여 의미가됩니다 경우를 누르면?

은 현재 모든 텍스트에 따라 계산하는 방법이 있나요? 위에 놓친 것이 있으면 알려주세요. 나는 광범위하게 다음 읽기 :

http://dennisreimann.de/blog/uitextview-height-in-uitableviewcell/

https://stackoverflow.com/questions/985394/growing-uitextview-and-uitableviewcell

UITextView inside a UITableViewCell

How do I size a UITextView to its content?

+0

UITableView를 사용하려는 특별한 이유가 있습니까? 간단한 3- 뷰 설정 (자동 레이아웃 사용)이 더 쉽고/좋을 것 같습니다. – jtbandes

+0

나는 현재의 디자인 제약 때문에이 방법을 사용했다. 그러나 그것은 별 차이가 없을 것입니다. 'contentHeight'가 잘못 계산되기 때문입니다. – KVISH

+0

또한 UITableView를 사용하면 적절한 줄 등으로 스크롤하는 것이 더 쉬워집니다. – KVISH

답변

2

내가와 위의 코드를 편집 한 다음 그것은 나를 위해 일하고 :

- (void)textViewDidChange:(UITextView *)textView {  
    // Get the number of lines in the current view 
    NSUInteger lines = textView.text.length; 
    if ((lines * 25) > currentContentHeight && (lines * 25) >= minimumContentHeight && bodyText.contentSize.height > minimumContentHeight) { 

     currentContentHeight = bodyText.contentSize.height; 

    } else if (lines < 5){ 
     currentContentHeight = minimumContentHeight; 
    } 

    [tblView beginUpdates]; 
    [tblView endUpdates]; 

    [bodyText setFrame:CGRectMake(5, 5, 310, currentContentHeight)]; 

    bodyText.selectedRange = [bodyText selectedRange]; 
} 

전화의 크기에 따라 처음에 을 minimumContentHeight과 동일하게 설정했습니다.