执行某些更改单元格(包括页眉/页脚单元格)高度的操作时,必须通知表格视图高度已更改。
通常通过以下任一方式完成
:
tableView.beginUpdates()
tableView.endUpdates()
或:
tableView.performBatchUpdates(_:completion:)
在这种情况下,您希望在文本视图中的文本更改时调用此方法-只需使用“回调”闭包即可轻松实现。
以下是在可重复使用的UITextView
中使用UITableViewHeaderFooterView
的示例。
这将适用于从XIB加载复杂的视图,但是由于该视图很简单(仅包含UITextView
),因此我们将全部通过代码来完成。此示例使用3个部分,每个部分包含12行(默认的表格视图单元格)。
首先,表视图控制器类-没有@IBOutlet
或@IBAction
连接,因此只需创建一个新的UITableViewController
并将其自定义类设置为MyTestSectionHeaderTableViewController
:
class MyTestSectionHeaderTableViewController: UITableViewController {
var myHeaderData: [String] = [
"Section 0","Section 1","Section 2",]
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 50
tableView.keyboardDismissMode = .onDrag
tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 75
tableView.register(UITableViewCell.self,forCellReuseIdentifier: "defCell")
tableView.register(MySectionHeaderView.self,forHeaderFooterViewReuseIdentifier: MySectionHeaderView.reuseIdentifier)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return myHeaderData.count
}
override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return 12
}
override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let c = tableView.dequeueReusableCell(withIdentifier: "defCell",for: indexPath)
c.textLabel?.text = "\(indexPath)"
return c
}
override func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
let v = tableView.dequeueReusableHeaderFooterView(withIdentifier: MySectionHeaderView.reuseIdentifier) as! MySectionHeaderView
v.myTextView.text = myHeaderData[section]
v.textChangedCallback = { txt in
self.myHeaderData[section] = txt
tableView.performBatchUpdates(nil,completion: nil)
}
return v
}
}
,这是UITableViewHeaderFooterView
类。请注意,它需要符合UITextViewDelegate
,因此我们可以告诉控制器文本已更改(因此可以在需要时更新高度),然后将新编辑的文本传回以更新数据源:
class MySectionHeaderView: UITableViewHeaderFooterView,UITextViewDelegate {
static let reuseIdentifier: String = String(describing: self)
var myTextView: UITextView = {
let v = UITextView()
v.isScrollEnabled = false
return v
}()
var textChangedCallback: ((String) -> ())?
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() -> Void {
contentView.addSubview(myTextView)
myTextView.translatesAutoresizingMaskIntoConstraints = false
let g = contentView.layoutMarginsGuide
NSLayoutConstraint.activate([
myTextView.topAnchor.constraint(equalTo: g.topAnchor),myTextView.leadingAnchor.constraint(equalTo: g.leadingAnchor),myTextView.trailingAnchor.constraint(equalTo: g.trailingAnchor),myTextView.bottomAnchor.constraint(equalTo: g.bottomAnchor)
])
myTextView.delegate = self
}
func textViewDidChange(_ textView: UITextView) {
guard let str = textView.text else {
return
}
textChangedCallback?(str)
}
}
结果:
本文链接:https://www.f2er.com/1990361.html