0

我正在尝试调整 UITableViewCell 中的 UITextView 的大小。我也想相应地调整 UITableViewCell 的大小。我的 UITableViewCell 正确调整大小,但 UITextView 没有。它最终只有两条线,而不是正确的大小。当我在 cellForRowAtIndexPath 方法中初始化它时,它看起来像:

        UITextView *notesTextView = [[UITextView alloc] initWithFrame:CGRectMake(83, 12, TEXTVIEW_WIDTH, 22)]; 

        notesTextView.tag = TEXTVIEW_TAG;
        notesTextView.delegate = self;

        [cell.contentView addSubview:notesTextView];

当我尝试在 textView 委托方法中修改它时,我会:

CGSize size = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(TEXTVIEW_WIDTH, 460) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"size: %@", NSStringFromCGSize(size)); // has the correct size
if (size.height > self.notesRowHeight) {
    self.notesRowHeight = size.height;
    UITextView *aTextView = (UITextView *)[self.view viewWithTag:TEXTVIEW_TAG];
    CGRect textViewFrame = CGRectMake(aTextView.frame.origin.x, aTextView.frame.origin.y, TEXTVIEW_WIDTH, size.height);
    aTextView.frame = textViewFrame;
    aTextView.contentSize = size;
    [aTextView sizeToFit];
    [aTextView sizeThatFits:size];
    NSLog(@"%@", NSStringFromCGRect(aTextView.frame)); // has the correct height

所以最终发生的是 tableView 正确调整大小,即使 textView 框架的大小正确(例如 {{83, 12}, {197, 120}}),textView 只有 2 行。它最终不会像我预期的那样占用 UITableViewCell 的大小。

编辑:当我尝试在不是 UITableViewCell 的普通 UIView 中调整 UITextView 的框架时,它工作得很好。所以我不确定在这种情况下有什么不同。

4

4 回答 4

4

我找到了答案。我不应该重新加载该行,而只是调用

[tableView beginUpdates];
[tableView endUpdates];

更多信息可以在以下位置找到: UITableViewCell 中的 UITextView 平滑自动调整大小显示和隐藏 iPad 上的键盘,但适用于 iPhone

于 2012-05-29T21:26:16.697 回答
1

我找到了解决这个问题的最佳方法。

首先,当然,您需要创建 UITextView 并将其添加到单元格的 contentView 中。我创建了一个名为“cellTextView”的 UITextView 实例变量这是我使用的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView fileNameCellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

    if (!cellTextView) {
        cellTextView = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 5.0, cell.bounds.size.width - 30.0, cell.bounds.size.height - 10.0)]; // I use these x and y values plus the height value for padding purposes.
    }
    [cellTextView setBackgroundColor:[UIColor clearColor]];
    [cellTextView setScrollEnabled:FALSE];
    [cellTextView setFont:[UIFont boldSystemFontOfSize:13.0]];
    [cellTextView setDelegate:self];
    [cellTextView setTextColor:[UIColor blackColor]];
    [cellTextView setContentInset:UIEdgeInsetsZero];
    [cell.contentView addSubview:cellTextView];

    return cell;
}

然后,创建一个名为 numberOfLines 的 int 变量,并在您的 init 方法中将该变量设置为 1。之后,在您的 textViewDelegate 的 textViewDidChange 方法中,使用以下代码:

- (void)textViewDidChange:(UITextView *)textView
{
    numberOfLines = (textView.contentSize.height / textView.font.lineHeight) - 1;

    float height = 44.0;
    height += (textView.font.lineHeight * (numberOfLines - 1));

    CGRect textViewFrame = [textView frame];
    textViewFrame.size.height = height - 10.0; //The 10 value is to retrieve the same height padding I inputed earlier when I initialized the UITextView
    [textView setFrame:textViewFrame];

    [self.tableView beginUpdates];
    [self.tableView endUpdates];

    [cellTextView setContentInset:UIEdgeInsetsZero];
}    

最后,将此代码粘贴到您的 heightForRowAtIndexPath 方法中:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = 44.0;
    if (cellTextView) {
        height += (cellTextView.font.lineHeight * (numberOfLines - 1));
    }
    return height;
}
于 2013-02-14T20:03:32.500 回答
0

我给你一个不一样的看法......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
       .
       .
       if(_YOUR_ROW_ && _YOUR_SECTION_)
       {

           cell.textView.text = Text_Of_TextView;
       }
       .
       .

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   .
   .
   if(_YOUR_ROW_ && _YOUR_SECTION_)
   {


       UIFont * FontSize = [UIFont systemFontOfSize:14.0];  
       CGSize textSize = [Text_Of_TextView sizeWithFont:FontSize constrainedToSize:CGSizeMake(Width_OF_textView, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

       return textSize.height+PADDING;
   }
   .
   .
}




-(void)textViewDidEndEditing:(UITextView *)textView
{

    [Text_Of_TextView setText:textView.text];


 CGRect frame = textViewInRow.frame;
        frame.size.height = textViewInRow.contentSize.height;
        textViewInRow.frame = frame;
 [TableView_InView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_YOUR_ROW_ inSection:_YOUR_SECTION_]] withRowAnimation:UITableViewRowAnimationNone];

}

并且不要忘记为 textview 实现自动调整大小的行为

于 2012-05-29T04:28:21.917 回答
0

这是一个简单的项目,演示了如何使用自动布局来调整 iOS 8 的单元格大小。几乎不需要特殊代码。有关关键点,请参阅 readme.md。

github上的项目

于 2015-06-05T05:31:15.337 回答