在UITableViewCell中调整UITextView的大小

时间:2012-05-29 03:47:41

标签: iphone uitextview

我正在尝试调整我在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 个答案:

答案 0 :(得分:4)

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

[tableView beginUpdates];
[tableView endUpdates];

更多信息可在以下网址找到: UITextView in a UITableViewCell smooth auto-resize shows and hides keyboard on iPad, but works on iPhone

答案 1 :(得分: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;
}

答案 2 :(得分: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实现自动调整大小行为

答案 3 :(得分:0)

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

project on github

相关问题