Is it possible to obtain a dynamic table view section header height using Auto Layout?

后端 未结 8 769
清歌不尽
清歌不尽 2020-11-28 02:36

New in iOS 8, you can obtain 100% dynamic table view cells by simply setting the estimated row height, then layout your elements in the cell using Auto Layout. If the conten

相关标签:
8条回答
  • 2020-11-28 03:31

    Swift 4+

    working(Tested 100%)

    If you need both section as well row with dynamic height based on content then you can use below code:

    On viewDidLoad() write this lines:

        self.globalTableView.estimatedRowHeight = 20
        self.globalTableView.rowHeight = UITableView.automaticDimension
    
        self.globalTableView.sectionHeaderHeight =  UITableView.automaticDimension
        self.globalTableView.estimatedSectionHeaderHeight = 25;
    

    Now we have set row height and section height by using UITableView Delegate methods:

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
       {
           return UITableView.automaticDimension
       }
       func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    
           return UITableView.automaticDimension
    
       }
    
    0 讨论(0)
  • 2020-11-28 03:34

    This can be accomplished by setting (or returning) the estimatedSectionHeaderHeight on your table view.

    If your section header is overlapping your cells after setting estimatedSectionHeaderHeight, make sure that you're using an estimatedRowHeight as well.

    (I'm adding this answer because the second paragraph contains an answer to an issue that can be found after reading through all of the comments which some might miss.)

    0 讨论(0)
提交回复
热议问题