UITextView在UITableViewCell中顺利自动调整大小 [英] UITextView in a UITableViewCell smooth auto-resize

查看:105
本文介绍了UITextView在UITableViewCell中顺利自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableViewCell内容视图中有一个UITextView,允许单元格自动调整,以便输入的文本完全显示 - 我想要完成的是一个自动调整单元格,就像本机iOS4联系人应用程序一样,当你输入notes时对于联系 - 即当textView的contentSize发生变化时 - 我调用reloadRowsAtIndexPaths并在委托的heightForRowAtIndexPath中为行提供新的高度 - 这可以完成工作,但它不像联系人的应用程序那样漂亮和流畅 - 我几乎可以肯定Apple在该应用程序中使用一些未记录的技巧,使单元格的contentView展开平滑和动画,而无需调用reloadRowsAtIndexPaths。我的问题是你如何建议实现这样的功能?我希望我没有错过任何解释细节。

I have a UITextView in a UITableViewCell contentview and allow the cell to autoresize so that the entered text is fully shown - what I am trying to accomplish is an autoresizing cell like the native iOS4 Contacts app has, when you enter "notes" for contact - i.e. when the contentSize of the textView changes - I call reloadRowsAtIndexPaths and in the delegate's heightForRowAtIndexPath I provide the new height for row - this does the job, however it is not nice and smooth like the contact's app - I am almost sure Apple uses some undocumented trick in that app to make the cell's contentView expand smooth and animated without calling reloadRowsAtIndexPaths. My question is how would you suggest to implement such functionality? I hope I didn't miss any details in explanation.

推荐答案

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

Try this code below, it will be help. You don't have to use any reload functions like reloadRowsAtIndexPaths.

// textview delegate

// textview delegate

- (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)];
    }
}

// tableview delegate

// tableview delegate

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

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

    return height;
}

这篇关于UITextView在UITableViewCell中顺利自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆