我已经尝试了下面的代码,它给出了正确的 TextView 框架高度
func textViewDidChange(_ textView: UITextView) {
myToDoList[keyArray[sect]]![row] = textView.text
var frame = textView.frame
frame.size.height = textView.contentSize.height
textView.frame = frame
print(frame)
let indexPath = self.tableView.indexPathForView(textView)
print(inputActive,indexPath)
self.tableView.indexPathForView(inputActive)
self.tableView.rowHeight = frame.size.height
}
最佳答案
“Krunal”的答案缺少一两 block ......
从单元格布局/约束开始:
并使用这段代码:
import UIKit
class WithTextViewCell: UITableViewCell, UITextViewDelegate {
@IBOutlet var theTextView: UITextView!
var callBack: ((UITextView) -> ())?
override func awakeFromNib() {
super.awakeFromNib()
// in case these were not set in IB
theTextView.delegate = self
theTextView.isScrollEnabled = false
}
func textViewDidChange(_ textView: UITextView) {
// tell controller the text changed
callBack?(textView)
}
}
class TableWithTextViewTableViewController: UITableViewController {
var cellData = [
"UITableViewController implements the following behaviors:",
"If a nib file is specified via the init(nibName:bundle:) method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.",
"If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.",
"When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear(_:). You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.",
"When the table view has appeared, the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear(_:).",
"It implements the superclass method setEditing(_:animated:) so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.",
"You create a custom subclass of UITableViewController for each table view that you want to manage. When you initialize the controller in init(style:), you must specify the style of the table view (plain or grouped) that the controller is to manage. Because the initially created table view is without table dimensions (that is, number of sections and number of rows per section) or content, the table view’s data source and delegate—that is, the UITableViewController object itself—must provide the table dimensions, the cell content, and any desired configurations (as usual). You may override loadView() or any other superclass method, but if you do be sure to invoke the superclass implementation of the method, usually as the first method call.",
]
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "WithTextViewCell", for: indexPath) as! WithTextViewCell
// Configure the cell...
cell.theTextView.text = cellData[indexPath.row]
cell.callBack = {
textView in
// update data source
self.cellData[indexPath.row] = textView.text
// tell table view we're starting layout updates
tableView.beginUpdates()
// get current content offset
var scOffset = tableView.contentOffset
// get current text view height
let tvHeight = textView.frame.size.height
// telll text view to size itself
textView.sizeToFit()
// get the difference between previous height and new height (if word-wrap or newline change)
let yDiff = textView.frame.size.height - tvHeight
// adjust content offset
scOffset.y += yDiff
// update table content offset so edit caret is not covered by keyboard
tableView.contentOffset = scOffset
// tell table view to apply layout updates
tableView.endUpdates()
}
return cell
}
}
“关键”部分:
向您的单元格添加一个“回调”闭包,以便我们可以在文本更改时通知 Controller 。
当回调发生时,让 TableView Controller :用编辑后的文本更新数据源;告诉 TextView 自行调整大小;并调整内容偏移量以避免插入符号(文本插入点)消失在键盘后面。
关于ios - 动态调整具有嵌入 swift 的 TextView 的 IOS tableview 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47033577/