Swift - how to make custom header for UITableView?

后端 未结 7 719
说谎
说谎 2020-12-23 13:57

I need to add custom header to my table

I try this

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    l         


        
相关标签:
7条回答
  • 2020-12-23 14:12

    If you are using custom cell as header, add the following.

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
            let headerView = UIView()
            let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
            headerView.addSubview(headerCell)
            return headerView
        }
    

    If you want to have simple view, add the following.

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView:UIView =  UIView()
        return headerView
    }
    
    0 讨论(0)
  • 2020-12-23 14:16

    add label to subview of custom view, no need of self.view.addSubview(view), because viewForHeaderInSection return the UIView

    view.addSubview(label)
    
    0 讨论(0)
  • 2020-12-23 14:18

    If you are willing to use custom table header as table header, try the followings....

    Updated for swift 3.0

    Step 1

    Create UITableViewHeaderFooterView for custom header..

    import UIKit
    
    class MapTableHeaderView: UITableViewHeaderFooterView {
    
        @IBOutlet weak var testView: UIView!
    
    }
    

    Step 2

    Add custom header to UITableView

        override func viewDidLoad() {
                super.viewDidLoad()
    
                tableView.delegate = self
                tableView.dataSource = self
    
                //register the header view
    
                let nibName = UINib(nibName: "CustomHeaderView", bundle: nil)
                self.tableView.register(nibName, forHeaderFooterViewReuseIdentifier: "CustomHeaderView")
    
    
        }
    
        extension BranchViewController : UITableViewDelegate{
    
        }
    
        extension BranchViewController : UITableViewDataSource{
    
            func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                return 200
            }
    
            func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
                let headerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "CustomHeaderView" ) as! MapTableHeaderView
    
                return headerView
            }
    
            func tableView(_ tableView: UITableView, numberOfRowsInSection section: 
    
        Int) -> Int {
                // retuen no of rows in sections
            }
    
            func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
                // retuen your custom cells    
            }
    
            func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
            }
    
            func numberOfSections(in tableView: UITableView) -> Int {
                // retuen no of sections
            }
    
            func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                // retuen height of row
            }
    
    
        }
    
    0 讨论(0)
  • 2020-12-23 14:20

    Did you set the section header height in the viewDidLoad?

    self.tableView.sectionHeaderHeight = 70
    

    Plus you should replace

    self.view.addSubview(view)
    

    by

    view.addSubview(label)
    

    Finally you have to check your frames

    let view = UIView(frame: CGRect.zeroRect)
    

    and eventually the desired text color as it seems to be currently white on white.

    0 讨论(0)
  • 2020-12-23 14:23

    This worked for me - Swift 3

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
            let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
            return headerCell
        }
    
    0 讨论(0)
  • 2020-12-23 14:27

    I have had some problems in Swift 5 with this. When using this function I had a wrong alignment with the header cell:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         let headerCell = tableView.dequeueReusableCell(withIdentifier: "customTableCell") as! CustomTableCell
         return headerCell
    }
    

    The cell view was shown with a bad alignment and the top part of the tableview was shown. So I had to make some tweak like this:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         let headerView = UIView.init(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 90))
         let headerCell = tableView.dequeueReusableCell(withIdentifier: "YOUR_CELL_IDENTIFIER")
         headerCell?.frame = headerView.bounds
         headerView.addSubview(headerCell!)
         return headerView
     }
    

    I am having this problem in Swift 5 and Xcode 12.0.1, I don't know if it is just a problem for me or it is a bug. Hope it helps

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