2012-08-13 50 views
1

在我的情况下,我将如何调整UITableViewCell以适应它包含的UITextView的高度?如何根据子视图正确调整父视图的大小? (例如,包含UITextView的UITableViewCell)

我目前正在努力实现这个使用下面的代码:

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

    // init the font that we use 
    UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16]; 

    if (indexPath.section == 0) { 

     // get the text size of the thread 
     CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 

     return textSize.height + 20; 

    } 

    // default 
    return 60; 

} 

我觉得如果我能得到这个方法访问UITableViewCell,然后我可以使用UITextViewcontentSize财产在其中,并返回。但我认为在执行流程的这一点上这是不可能的。

我不能只是调整缓冲区,因为它不会缩放get size函数返回的文本大小。文字越短,缓冲区的大小越大。

这是调整UITableViewCell的高度,但它没有在其中显示UITextView。我已经在sizeWithFont:constrainedToSize:lineBreakMode:方法中指定UITextView的宽度,即280。我已经指定我希望基本上没有最大高度,以便它可以使用常量来尽可能多地进行文字包装。我将20单元的缓冲区添加到方法调用的结果高度,因为在UITextView之上有一个20单元缓冲区,在我的Storyboard中,我还想在其下面有一个20单元缓冲区。尽管如此,所有这些的最终结果仍然有文本被剪掉。下面是我在谈论的一个画面:

clipped cell contents

任何想法?谢谢!

回答

1

我打算继续回答我的问题。对此的回答分两步进行。

首先,在tableView:cellForRowAtIndexPath:方法的内部逻辑期间调整UITextView的大小。请确保在对UITextView中的文字进行任何更改后执行此操作。我使用下面的方法重新大小我UITextView,里面的UITableViewCell一个子类,其中self.text是文本观点:

- (void)resizeTextView { 

    // get the current frame size 
    CGRect frame = self.text.frame; 

    // set it to the height of the text view 
    frame.size.height = self.text.contentSize.height; 

    // set the new size of the text view 
    self.text.frame = frame; 

} 

接下来,我指定的tableView:heightForRowAtIndexpath:方法内的UITableViewCell的高度。这就是大部分神秘的地方,因为为了获得这一行的高度,你必须计算将要在行中显示的任何文本的高度,以及计算高度所需的任何其他内容。在我的情况下,是一个UITextView。如果不能访问此方法中的UITableViewCell实例,则不得不使用类似sizeWithFont:constrainedToSize:lineBreakMode:之类的内容。但是,我注意到这并没有给我带来一致的高度。或者至少,不是高度似乎为UITextView我的文字留下了足够的空间。虽然这是问题。文本视图具有附加的左右填充以及不同的线高度,或者与sizeWithFont方法使用的不同。

所以,捏造sizeWithFont方法的结果的一段时间后,我结束了以下代码:

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

    // init the font that we use 
    UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16]; 

    if (indexPath.section == 0) { 

     // get the text size of the thread 
     CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 

     // text height + padding 
     return textSize.height + 40; 

    } else if (indexPath.section == 1) { 

     // get the comment for this row 
     Comment *comment = [_comments objectAtIndex:indexPath.row]; 

     // get the heights of the text areas 
     CGSize textSize = [comment.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 

     return textSize.height + 40; 

    } 

    return 60; 

} 

在尺寸约束参数,我减去16个单位,因为UITextView具有大约一个填充左侧和右侧共有8个单位。这将使我们更接近我们对文本高度的期望。

我还添加了一些填充到40个单位的最终高度,因为该行本身需要在UITextView周围填充一些填充。

你走了!希望有所帮助。

相关问题