Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在具有动态高度和最大高度的UITableViewCell中添加UITextView_Ios_Swift_Uitextview - Fatal编程技术网

Ios 在具有动态高度和最大高度的UITableViewCell中添加UITextView

Ios 在具有动态高度和最大高度的UITableViewCell中添加UITextView,ios,swift,uitextview,Ios,Swift,Uitextview,我有一个UITableViewCell,里面有一个UITextView。UITextView添加到情节提要中,并设置了所有必要的约束。UITextView的高度应响应内部内容,但仅响应到最大高度,然后UITextView应停止增长并可滚动。 我已经用下面的代码归档了。我的问题是,如果我再次从UITextView中删除行,UITextView将不会收缩,或者它会收缩,因此高度太小。我该怎么办 /// Check if height of UITextView is bigger than maxi

我有一个UITableViewCell,里面有一个UITextView。UITextView添加到情节提要中,并设置了所有必要的约束。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内容视图仅包含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)
    }
  }

}

很有趣。实际上我花了一个小时试着这么做。我快到了,但有个小故障。好问题。我还发现了一个小问题,UITextView的高度突然变得超小…@Esera你找到解决方案了吗?你提供的解决方案很有效,但它不是我想要的:如果达到最大高度,我想使UITextView可滚动。这正是当你添加超过最大高度的行时发生的情况。你看到了什么结果?