Swift /如何使用动态高度的UITextView INSIDE UITableViewCell具有动态高度 [英] Swift / how to use UITextView with dynamic height INSIDE UITableViewCell with dynamic height

查看:206
本文介绍了Swift /如何使用动态高度的UITextView INSIDE UITableViewCell具有动态高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码让UITableViewCell具有动态高度:

I'm using the following code to have the UITableViewCell have a dynamic height:

tableView.estimatedRowHeight = 155
tableView.rowHeight = UITableViewAutomaticDimension

这段代码让UITextView根据内容改变它的高度。

And this code to have the UITextView change it's height based on the content.

extension NewUserTweetTableVC: UITextViewDelegate {

    func textViewDidChange(textView: UITextView) {
        let fixedWidth = textView.frame.size.width
        textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
        let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
        var newFrame = textView.frame
        newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
        textView.frame = newFrame    
    }
}

我在运行时更改了UITextView的文本, 高度 UITextView的更改,但UITableViewCell的高度没有。我怎么能改变呢?非常感谢帮助。

Once I change the text of the UITextView during runtime, the height of the UITextView changes, but the height of the UITableViewCell does not. How can I change that? Help is very appreciated.


推荐答案

以编程方式获取textview的高度...

Get programmatically height of textview...

let textView = UITextView()//your Text view
let sizeThatShouldFitTheContent = textView.sizeThatFits(textView.frame.size)
let height = sizeThatShouldFitTheContent.height

试试此演示......

Try this demo...

自行调整大小的UITextView UITableView

http://candycode.io/self-sizing-uitextview-in-a-uitableview-using-auto- layout-like-reminders-app /

这篇关于Swift /如何使用动态高度的UITextView INSIDE UITableViewCell具有动态高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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