运行时动态节标题高度

我在{@ {1}}的视图控制器中有一个节标题,在标题中,我有一个UITableView并禁用了滚动,并将所有UITextView的边固定到了其超级视图。{{3 }}

这是自动更改高度的代码

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 的文本超过75的高度时,此后无论UITextView内容如何,​​它都不会改变。因此,我是否需要添加任何内容以确保表节标题的高度根据UITextView的内容进行了更改?我在这里想念什么吗?

iCMS 回答:运行时动态节标题高度

执行某些更改单元格(包括页眉/页脚单元格)高度的操作时,必须通知表格视图高度已更改。

通常通过以下任一方式完成

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)
    }
    
}

结果:

enter image description here

本文链接:https://www.f2er.com/1990361.html

大家都在问