如何根据textview调整表格单元格的大小? [英] How to resize table cell based on textview?

查看:112
本文介绍了如何根据textview调整表格单元格的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义 UITableViewCell 中有一个 UITextView 。 textview委托在tableviewcell自定义类中分配。 Textview滚动已禁用。文本加载到每个文本视图中并且是多行的。但是文本总是被剪切,因为单元格高度不会改变。

I have a UITextView in a custom UITableViewCell. The textview delegate is assigned in the tableviewcell custom class. Textview scrolling is disabled. Text loads into each textview and is multiline. But the text is always clipped because the cell height doesn't change.

我在 viewDidLoad 中有以下内容: tableview控制器:

I have the following in viewDidLoad of the tableview controller:

tableView.estimatedRowHeight = 56.0 
tableView.rowHeight = UITableViewAutomaticDimension

知道为什么这不起作用吗?

Any idea why this isn't working?

推荐答案

完美地尝试我的回答。

var record : NSArray = NSArray()
var  hight: CGFloat = 0.0

将此代码放入viewDidLoad()

record = ["I have a UITextView in a custom UITableViewCell. The textview delegate is assigned in the tableviewcell custom class." ,"Textview scrolling is disabled. Text loads into each textview and is multiline. But the text is always clipped because the cell height doesn't change.","I have the following in viewDidLoad of the tableview controller:"," have a UITextView in a custom UITableViewCell. The textview delegate is assigned in the tableviewcell custom class.","Textview scrolling is disabled. Text loads into each textview and is multiline. But the text is always clipped because the cell height doesn't change.","I have the following in viewDidLoad of the tableview controller:","i just give you one link at put place i use label and you can now use your textview and give same constrain that i give in that link and try it so your problem will be solve","I have the following in viewDidLoad of the tableview controller:"];

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return record.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Textviewcell", forIndexPath: indexPath)

    let textview: UITextView = (cell.viewWithTag(5) as! UITextView)

    textview.text = record.objectAtIndex(indexPath.row) as? String

    return cell

}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    // 7.1>
   hight = self.findHeightForText(self.record.objectAtIndex(indexPath.row) as! String, havingWidth: self.view.frame.size.width - 10, andFont: UIFont.systemFontOfSize(14.0)).height
   return 44 + hight
}



func findHeightForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize {

    var size = CGSizeZero
    if text.isEmpty == false {
        let frame = text.boundingRectWithSize(CGSizeMake(widthValue, CGFloat.max), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
        size = CGSizeMake(frame.size.width, ceil(frame.size.height))
        }
        return size
}

在Swift 3.0中

func findHeightForText(text: String, havingWidth widthValue: CGFloat, andFont font: UIFont) -> CGSize {

        var size = CGSize.zero 
        if text.isEmpty == false {
            let frame = text.boundingRect(with: CGSize(width: widthValue, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
            size = CGSize(width: frame.size.width, height: ceil(frame.size.height))//CGSizeMake(frame.size.width, ceil(frame.size.height))
        }
        return size
    }

以下是一些屏幕截图。故事板

Here are some screen shot .Storyboard

使用UITextView的运行时表视图

这篇关于如何根据textview调整表格单元格的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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