键入时调整包含 UITextView 的 UITableViewCell 的大小 [英] Resize UITableViewCell containing UITextView upon typing

查看:14
本文介绍了键入时调整包含 UITextView 的 UITableViewCell 的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含自定义单元格的 UITableViewController.每个单元格都是使用 nib 创建的,并包含一个不可滚动的 UITextView.我在每个单元格内添加了约束,以便单元格使其高度适应 UITextView 的内容.所以最初我的控制器看起来像这样:

现在我希望当用户在单元格中键入内容时,其内容会自动适应.这个问题已经被问过很多次了,特别看

如果我向下和向上滚动 tableView 以强制调用 cellForRowAtIndexPath,我会恢复单元格的正确高度:

请注意,我确实没有实现 heightForRowAtIndexPath,因为我希望 autoLayout 能够解决这个问题.

有人可以告诉我我做错了什么或在这里帮助我吗?非常感谢!

解决方案

这是一个对我来说很好的快速解决方案.如果您使用自动布局,则需要为estimatedRowHeight 分配一个值,然后返回UITableViewAutomaticDimension 作为行高.最后在文本视图委托中执行类似于下面的操作.

覆盖 func viewDidLoad() {super.viewDidLoad()self.tableView.estimatedRowHeight = 44.0}覆盖 func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) ->CGF浮动{返回 UITableViewAutomaticDimension}//MARK: UITextViewDelegatefunc textViewDidChange(textView: UITextView) {//计算文本视图是否会改变高度,然后只强制//如果更新,则要更新的表.同时禁用动画//防止jankness".让 startHeight = textView.frame.size.heightlet calcHeight = textView.sizeThatFits(textView.frame.size).height//仅限iOS 8+如果 startHeight != calcHeight {UIView.setAnimationsEnabled(false)//禁用动画self.tableView.beginUpdates()self.tableView.endUpdates()//如果滚动,可能需要在此处插入其他内容//以一种意想不到的方式表.这滚动到底部//表的.(虽然你可能需要更多的东西//如果在中间编辑会很复杂.)让 scrollTo = self.tableView.contentSize.height - self.tableView.frame.size.heightself.tableView.setContentOffset(CGPoint(x: 0, y: scrollTo), 动画: false)UIView.setAnimationsEnabled(true)//重新启用动画.}

I have an UITableViewController that contains a custom cell. Each cell was created using a nib and contains a single non-scrollable UITextView. I have added constraints inside each cell so that the cell adapts its height to the content of the UITextView. So initially my controller looks like this :

Now I want that when the user types something in a cell its content automatically adapts. This question has been asked many times, see in particular this or the second answer here. I have thus written the following delegate in my code :

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    return YES;
}

However it leads to the following strange behavior : all constraints are ignored and all cells height collapse to the minimal value. See the picture below:

If I scroll down and up the tableView in order to force for a new call of cellForRowAtIndexPath, I recover the correct heights for the cells:

Note that I did not implement heightForRowAtIndexPath as I expect autoLayout to take care of this.

Could someone tell me what I did wrong or help me out here ? Thank you very much !

解决方案

Here is a swift solution that is working fine for me. Provided you are using auto layout, you need assign a value to estimatedRowHeight and then return UITableViewAutomaticDimension for the row height. Finally do something similar to below in the text view delegate.

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.estimatedRowHeight = 44.0
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

// MARK: UITextViewDelegate
func textViewDidChange(textView: UITextView) {

    // Calculate if the text view will change height, then only force 
    // the table to update if it does.  Also disable animations to
    // prevent "jankiness".

    let startHeight = textView.frame.size.height
    let calcHeight = textView.sizeThatFits(textView.frame.size).height  //iOS 8+ only

    if startHeight != calcHeight {

        UIView.setAnimationsEnabled(false) // Disable animations
        self.tableView.beginUpdates()
        self.tableView.endUpdates()

        // Might need to insert additional stuff here if scrolls
        // table in an unexpected way.  This scrolls to the bottom
        // of the table. (Though you might need something more
        // complicated if editing in the middle.)

        let scrollTo = self.tableView.contentSize.height - self.tableView.frame.size.height
        self.tableView.setContentOffset(CGPoint(x: 0, y: scrollTo), animated: false)

        UIView.setAnimationsEnabled(true)  // Re-enable animations.
}

这篇关于键入时调整包含 UITextView 的 UITableViewCell 的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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