简体   繁体   English

如何根据UITextView子视图的大小调整UITableViewCell的大小?

[英]How do I resize a UITableViewCell according to the size of a UITextView subView?

Currently I can successfully resize my UITextView according to the amount of text within it. 目前,我可以根据其中的文本量成功调整UITextView的大小。

My problem is that this UITextView is within a UITableViewCell. 我的问题是此UITextView在UITableViewCell中。 What I've tried to do is use the height of the resized UITextView to resize the cell by accessing it's frame and setting the height of that. 我试图做的是通过访问已调整大小的UITextView的高度并访问其框架并设置其高度来调整其大小。

This is my code: 这是我的代码:

          //dynamically resize textview and cell based on content
                self.aboutMeTextView.text = profile["aboutMe"] as String
                self.aboutMeTextView.sizeToFit()
                self.aboutMeTextView.layoutIfNeeded()
                var frame = self.aboutMeTextView.frame as CGRect
                frame.size.height = self.aboutMeTextView.contentSize.height
                self.aboutMeTextView.frame = frame

                var cellFrame = self.aboutMeCell.frame as CGRect
                cellFrame.size.height = self.aboutMeTextView.contentSize.height * 2

                self.aboutMeCell.frame = cellFrame

It just doesn't work properly. 它只是无法正常工作。 The textView resizes but the cell doesn't resize properly and also my scrollView won't even scroll down enough for me to see the whole of the resized textview. textView调整了大小,但是单元格没有正确调整大小,并且我的scrollView甚至没有向下滚动到足以让我看到整个调整后的textview的大小。 I'm guessing if I can successfully set the cells height, the scrollView height will automatically adjust. 我猜如果我可以成功设置单元格的高度,scrollView的高度将自动调整。

I've looked at similar questions but they haven't helped me. 我看过类似的问题,但它们并没有帮助我。

Would appreciate some help. 希望能有所帮助。

Thanks for your time 谢谢你的时间

You can use the UITableView -method heightForRowAtIndexPath 您可以使用UITableView -method heightForRowAtIndexPath

API Documentation API文档

It returns the height for a row. 它返回一行的高度。 So you can use the label from the current cell and set the height of the cell to the height of the label: 因此,您可以使用当前单元格中的标签,并将单元格的高度设置为标签的高度:

override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    var cellID = "Cell"
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellID)  as UITableViewCell

    var yourLabelHeight = cell.yourLabel.size.height
    return yourLabelHeight
}

You can call UITableView.beginUpdates() and UITableView.endUpdates() like so: 您可以像这样调用UITableView.beginUpdates()和UITableView.endUpdates():

    extension TableViewCell: UITextViewDelegate {

        var delegate: TableViewController!
        var row: Int!

        func textViewDidChange(_ textView: UITextView) {

            delegate.tableView.beginUpdates()
            delegate.tableView.endUpdates()

        }

    }


    class TableViewController: UITableViewController {

        override func viewDidLoad() {
            super.viewDidLoad()

            tableView.rowHeight = 44
            tableView.rowHeight = UITableViewAutomaticDimension
            tableView.estimatedRowHeight = 44

        }

        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell

            cell.delegate = self
            cell.row = indexPath.row

            return cell

        }

    }

Be sure your constraints are set up for your text view in the table view cell. 确保在表格视图单元格中为文本视图设置了约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM