UITextView在UITableViewCell中顺利自动调整大小

jus*_*mer 14 iphone cocoa-touch ios4

我有一个UITableViewCell内容查看一个UITextView并允许细胞自动调整,使输入的文本将尽显 - 我试图做到的是像本地的iOS4联系人应用程序的自动尺寸电池有,当你输入"笔记"的接触 - 即当TextView的变化contentSize - 我称之为reloadRowsAtIndexPaths并在代理的heightForRowAtIndexPath我为行提供新的高度 - 这并不工作,但它是不漂亮,光滑如联系人的应用程序 - 我几乎可以肯定苹果使用了一些无证在该应用程序中的技巧,使单元格的contentView展开平滑和动画,而无需调用reloadRowsAtIndexPaths.我的问题是你如何建议实现这样的功能?我希望我没有错过任何解释细节.

小智 16

试试下面的代码,这将有所帮助.您不必使用任何重装函数,如reloadRowsAtIndexPaths.

// textview委托

- (void)textViewDidChange:(UITextView *)textView {
    if (contentView.contentSize.height > contentRowHeight) {

    contentRowHeight = contentView.contentSize.height;

    [theTableView beginUpdates];
    [theTableView endUpdates];

    [contentView setFrame:CGRectMake(0, 0, 300.0, contentView.contentSize.height)];
    }
}
Run Code Online (Sandbox Code Playgroud)

// tableview委托

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

    if (indexPath.row == 0)
        height = kTitleRowHeight;
    else 
        height = contentRowHeight;

    return height;
}
Run Code Online (Sandbox Code Playgroud)


Hec*_*tos 11

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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;
}
Run Code Online (Sandbox Code Playgroud)

然后,创建一个名为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];
}    
Run Code Online (Sandbox Code Playgroud)

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = 44.0;
    if (cellTextView) {
        height += (cellTextView.font.lineHeight * (numberOfLines - 1));
    }
    return height;
}
Run Code Online (Sandbox Code Playgroud)