2014-04-07 35 views
0

我試圖改變一個UITableViewCell內UITextView的高度,我有搜索了很多關於SO,有很多答案,其中有許多是非常相似的。我認爲最好的答案我發現是這樣的:更改動態UITextView的高度內的UITableViewCell

UITableViewCell with UITextView height in iOS 7?

我都試過了,但沒有很好地工作,這個我我的代碼:

- (CGFloat)textViewHeightForRowAtIndexPath: (NSIndexPath*)indexPath { 
    UITextView *calculationView = [self.textViews objectForKey: indexPath]; 
    CGFloat textViewWidth; 
    if (!calculationView.attributedText) { 
     // This will be needed on load, when the text view is not inited yet 

     calculationView = [[UITextView alloc] init]; 
     calculationView.attributedText = [[NSAttributedString alloc] initWithString:[[self.comments objectAtIndex:indexPath.row] valueForKey:@"comment"] attributes:self.regularDict];// get the text from your datasource add attributes and insert here 
     textViewWidth = 250; // Insert the width of your UITextViews or include calculations to set it accordingly 
    } else { 
     textViewWidth = calculationView.frame.size.width; 
    } 
    CGSize size = [calculationView sizeThatFits:CGSizeMake(textViewWidth, FLT_MAX)]; 
    return size.height; 
} 

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

return [self textViewHeightForRowAtIndexPath:indexPath]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CommentCell *cell = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    NSAttributedString *text_user = [[NSAttributedString alloc] initWithString:[[self.comments objectAtIndex:indexPath.row] valueForKey:@"comment"] attributes:self.regularDict]; 

    [cell.my_text setAttributedText:text_user]; 
    [cell.my_text setTextAlignment:NSTextAlignmentRight]; 

    CGRect frame = cell.my_text.frame; 

     CGSize size = [cell.my_text sizeThatFits:CGSizeMake(250, FLT_MAX)]; 

frame.size.height = size.height; 

    [cell.my_text setFrame:frame]; 
    [self.textViews setObject:cell.my_text forKey:indexPath]; 

    return cell; 
} 

但這是結果:

enter image description here

有很多的行空的空間,我不明白爲什麼...

任何人都可以幫助我嗎?

回答

0
你嘗試什麼實現將是有益的,但是

更多信息...

我想你可能會發現,tableView:heightForRowAtIndexPath:運行完成之前tableView:cellForRowAtIndexPath:,因此任何代碼改變內UITextView的高度tableView:(UITableView *)tableView cellForRowAtIndexPath:將無效。

爲了清楚起見,所有的行高之前UITableView設置在該行內的單元格的內容進行填充。

所以當使用tableView:heightForRowAtIndexPath:時,在這個方法中建立每個單元的高度在indexPath是很重要的。

0

我有這個問題一次,它結束了,因爲我使用的字體沒有要求像「sizeThatFits」返回高度一樣多的高度。從那時起,即時攜帶以下方法幫助我,每當我需要它:

- (CGFloat)heightOfTextViewWithString:(NSString *)string 
          withFont:(UIFont *)font 
         andFixedWidth:(CGFloat)fixedWidth 
{ 
    UITextView *tempTV = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, fixedWidth, 1)]; 
    tempTV.text = [string uppercaseString]; 
    tempTV.font = font; 

    CGSize newSize = [tempTV sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)]; 
    CGRect newFrame = tempTV.frame; 
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height); 
    tempTV.frame = newFrame; 

    return tempTV.frame.size.height; 
} 
相關問題