【问题标题】:How do I resize a UITableViewCell according to the size of a UITextView subView?如何根据 UITextView 子视图的大小调整 UITableViewCell 的大小?
【发布时间】:2015-03-27 23:58:02
【问题描述】:

目前我可以根据其中的文本量成功调整 UITextView 的大小。

我的问题是这个 UITextView 在 UITableViewCell 中。我试图做的是使用调整后的 UITextView 的高度通过访问它的框架并设置它的高度来调整单元格的大小。

这是我的代码:

          //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

它只是不能正常工作。 textView 调整大小,但单元格没有正确调整大小,而且我的 scrollView 甚至不会向下滚动到足以让我看到整个调整大小的 textview。我猜如果我可以成功设置单元格高度,scrollView高度会自动调整。

我看过类似的问题,但它们没有帮助我。

不胜感激。

感谢您的宝贵时间

【问题讨论】:

标签: ios uitableview swift uitextview


【解决方案1】:

你可以使用UITableView-方法heightForRowAtIndexPath

API Documentation

它返回一行的高度。因此,您可以使用当前单元格中的标签并将单元格的高度设置为标签的高度:

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
}

【讨论】:

  • 我应该提到它是一个静态单元格。我尝试使用返回的 indexPath 来设置特定的行高,但是我得到了一堆约束错误
  • 这还能用吗?我想我无法在我的 tableView 的 heightForRow 中创建一个单元格。我使用的是 tableView 而不是 tableView 控制器。
  • heightForRow 委托方法中将单元格出列不是一个好的解决方案。您可以通过提供字体和字体大小来计算字符串的大小。
【解决方案2】:

你可以像这样调用 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

        }

    }

确保为表格视图单元格中的文本视图设置了约束。

【讨论】:

  • 这个解决方案只有在UILabel的情况下才有效
  • @SyedQamarAbbas 看起来我让它以这种方式工作。你确定吗?我的解决方案会导致什么错误?
  • @SyedQamarAbbas 确保使用textView.isScrollEnabled = false 禁用UITextView 上的滚动
猜你喜欢
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-08
相关资源
最近更新 更多