Ios 运行时动态节标题高度
在视图控制器中,我有一个Ios 运行时动态节标题高度,ios,swift,uitableview,uitextview,uitableviewsectionheader,Ios,Swift,Uitableview,Uitextview,Uitableviewsectionheader,在视图控制器中,我有一个UITableView,其中有一个UITextView,禁用滚动,并将所有UITextView边缘固定到其超级视图 这是自动高度变化的代码 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { guard let view = tableView.dequeueReusableHeaderFooterView(withIdentif
UITableView
,其中有一个UITextView
,禁用滚动,并将所有UITextView
边缘固定到其超级视图
这是自动高度变化的代码
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: "CreatePostHeaderView") as? CreatePostHeaderView else {
return nil
}
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}
并使用此代码设置估计高度
tableView.estimatedSectionHeaderHeight = 75
但是在运行时,当
UITextView
的文本超过75的高度时,无论UITextView的内容如何,它都不会发生变化。因此,我是否需要添加任何内容来确保表节标题高度根据UITextView
内容进行更改?我在这里遗漏了什么吗?当执行某些更改单元格高度(包括页眉/页脚单元格)的操作时,必须通知表视图高度已更改
这通常通过以下两种方式完成:
tableView.beginUpdates()
tableView.endUpdates()
或:
在这种情况下,您希望在文本视图中的文本发生更改时调用此函数,这可以通过“回调”闭包轻松完成
下面是在可重用的UITableViewHeaderFooterView
中使用UITextView
的示例
这将适用于从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)
}
}
结果是:
我不太确定这是否有效,因为我对我的tableHeaderView执行了这项操作,但从未尝试过对节头执行此操作:
view.frame=CGRect(原点:CGPoint.zero,大小:view.systemLayoutSizeFitting(大小,withHorizontalFittingPriority:UILayoutPriority.defaultLow,verticalFittingPriority:UILayoutPriority.fittingSizeLevel))
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)
}
}