ios - 在具有最大高度的动态高度的 UITableViewCell 中添加 UITextView

标签 ios swift uitextview

我有一个 UITableViewCell,里面有一个 UITextView。 UITextView 添加到 Storyboard中,并设置所有必要的约束。 UITextView 的高度应该响应内部的内容,但只能达到最大高度,然后 UITextView 应该停止增长并且可以滚动。 我已经用下面的代码实现了这一点。我的问题是,如果我再次从我的 UITextView 中删除行,UITextView 不会收缩,或者它会收缩,所以高度太小。我该怎么办?

/// Check if height of UITextView is bigger than maximum
if Int(textView.frame.size.height) >= 75 {
    textView.isScrollEnabled = true
    textView.frame.size.height = 74
}
else {
    textView.isScrollEnabled = false

    /// Change the UITableViewCells height if the UITextView did change
    let currentOffset = controller.tableView.contentOffset

    UIView.setAnimationsEnabled(false)

    controller.tableView.beginUpdates()
    controller.tableView.endUpdates()

    UIView.setAnimationsEnabled(true)

    controller.tableView.setContentOffset(currentOffset, animated: false)
}

最佳答案

我决定试一试,效果很好。它适用于仅返回 1 行的 UITableView,因此您需要做更多的工作来跟踪真实应用程序中的单元格。

这是 UITableViewCell 类:

import UIKit
class Cell: UITableViewCell {
    @IBOutlet weak var textView: UITextView!
}

Cell.xib 内容 View 仅包含 UITextView。我将行高设置为 44,然后设置 TextView.top = top,bottom = TextView.bottom,trailing = TextView.trailing + 36,TextView.leading = leading + 36 的约束。前导和尾随约束的 36 不是重要的是,我只是想要两侧有一些空间。

这是整个 ViewController:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {

  @IBOutlet weak var tableView: UITableView!
  private var textViewHeight: CGFloat = 0.0

  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.rowHeight = 44
    tableView.tableFooterView = UIView()
    tableView.register(UINib(nibName: "Cell", bundle: nil), forCellReuseIdentifier: "cell")
  }

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
  }

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell
    cell.textView.delegate = self
    cell.textView.text = ""
    cell.textView.layer.borderColor = UIColor.lightGray.cgColor
    cell.textView.layer.borderWidth = 1
    cell.textView.layer.cornerRadius = 4
    return cell
  }

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return min(75, max(textViewHeight, 44))
  }

  func textViewDidChange(_ textView: UITextView) {
    let size = textView.bounds.size
    let newSize = textView.sizeThatFits(CGSize(width: size.width, height: CGFloat.greatestFiniteMagnitude))
    if size.height != newSize.height {
      textViewHeight = newSize.height
      UIView.setAnimationsEnabled(false)
      tableView.beginUpdates()
      tableView.endUpdates()
      UIView.setAnimationsEnabled(true)
    }
  }

}

关于ios - 在具有最大高度的动态高度的 UITableViewCell 中添加 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54673105/

相关文章:

ios - 如何使用泛型协议(protocol)作为变量类型

objective-c - segmentedControl 和 TextView 刷新/重新加载 - xcode

android - Cordova - 如何检测是否安装了谷歌地图应用程序

ios - 从 Firebase 删除后 Swift 3 UITableView 不更新

ios - 如何继承UIStoryboard

Swift UICollectionview Horizo​​ntal Cell 显示中心位置并根据单元格选择自动移动

iphone - 为什么SBJson JSON解析只得到最后一个感兴趣的key?

swift : cannot convert return expression of any view to return type some view

ios - 在 UITextView 委托(delegate)事件之前调用的键盘事件

ios - 如何在 UITextView 中绘制文本