UITextView和单元格根据textview的内容动态更新高度? [英] UITextView and cell dynamically update height based on content of textview?

查看:168
本文介绍了UITextView和单元格根据textview的内容动态更新高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义单元格中遇到了有关UITextView的问题。除了我的问题,textView完美地运行。
我有UITextViewDelegate方法设置,所以当textview发生某些事情时,我将这些方法连接起来。

I have been having an issue regarding a UITextView inside a custom cell. The textView works perfectly besides my issue. I have the UITextViewDelegate methods setup so when something happens to the textview, I have those methods connected.

我想知道如何让我的自定义单元格在用户输入时动态增加它的高度。一旦用户点击文本视图行的末尾,也可以让textview自动添加另一行。
我在网上搜索了一段时间,这个问题的大多数例子都是客观的c。没有太多的快速代码。我将不胜感激任何帮助。

I would like to know how I can have my custom cell dynamically increase it's height as the user types. Once the user hits the end of the line of the textview, to also have the textview add another line automatically. I have been searching online for a while, and most of the examples for this issue are in objective c. Not much swift code. I would appreciate any help.

我将我的单元格挂在tableview文件中......

I have my cell hooked up in the tableview file as such..

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

      let cell: cellCustom =  the_TableView.dequeueReusableCellWithIdentifier("cell") as! cellCustom

        return cell
 }

在自定义单元格文件中,我有代表设置...

In the custom cell file, I have the delegates set up...

func textViewDidChange(textView: UITextView) {


    }


func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {


        return true
    }

我尝试了一些像......

I have tried some things like ...

self.textview.sizeToFit() 

self.the_TableView.rowHeight = UITableViewAutomaticDimension
        self.the_TableView.estimatedRowHeight = 47

当我这样做时, textview占用了所有文本,但是在我辞去它的第一个响应者并向上滚动以便单元格消失之前,它没有增加大小。然后当单元格反弹时,它会更新。

When I did that, the textview took all the text, but did not increase in size until I resigned it's first responder and scrolled up for the cell to be gone. Then when the cell bounces back down, it updates.

我已经添加了所有约束,项目没有风险或错误,并且工作正常。

I have already added all the constraints and the project has zero risks or errors and works perfectly.

为了澄清,我希望uitextview根据用户在键入内容时输入的内容来更改高度,这意味着自定义单元格和tableview必须更新。想想iPhone上的清晰应用程序。就像那样。单元格在用户输入时动态更新,高度增加,同时在用户到达结尾时添加新行。这就是我要达到的目标。

To clarify, I would like the uitextview to change height based on the content the user types while they are typing it, meaning the custom cell and tableview would have to update. Think of the clear app on iPhone. It's like that. The cell updates dynamically as the user types, increasing in height, while adding a new line once the user reaches the end. That is what I'm reaching for.

虽然我不是新手,但我从来没有真正开发过这一点。任何帮助将非常感激。由于我将自定义单元格放在另一个文件中,因此无法从tableview文件中访问我的单元格元素。我猜还是新手。感谢您阅读本文。

Though I am not new to developing by any means, I never really got to this point in development before. Any help would be extremely appreciated. Since I have my custom cell in another file, I have difficulty accessing my cell elements from the tableview file. Still a novice I guess. Thanks for reading this.

推荐答案

这适用于ios 7

这进入你的tableviewcell

this goes into your tableviewcell

protocol HeightForTextView {
    func heightOfTextView(height: CGFloat)

}

class TableViewCell: UITableViewCell {

    @IBOutlet weak var textView: UITextView!

    var delgate:HeightForTextView?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func textViewDidChange(textView: UITextView) {

        var fixedWidth: CGFloat = textView.frame.size.width
        var newSize: CGSize = textView.sizeThatFits(CGSizeMake(fixedWidth, CGFloat.max))

        if let iuDelegate = self.delgate {

            iuDelegate.heightOfTextView(newSize.height)
        }


    }

}




你的tableview控制器应该是

your tableview controller should be



 class TableViewController: UITableViewController,HeightForTextView {

        var textViewHeight = CGFloat()

    //In your cell for row method set your your delegate 
 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! TableViewCell
    cell.delgate = self

    return cell
}


//delegate method

    func heightOfTextView(height: CGFloat) {

    textViewHeight = height
    self.tableView.beginUpdates()
    self.tableView.endUpdates()

    }

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

            return textViewHeight + 70
    }

    }

这篇关于UITextView和单元格根据textview的内容动态更新高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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