swift 設置tableHeaderView 和tableFooterView


1. headerView 更具數據源動態改變高度

 

 

 

 

2.

tableFooterView更具數據源改變高度

 

 

 

 

 

3. 設置TableHeaderView

// ==================== 分 ==================== 割 ==================== 線 ====================
///退款狀態控制器的TableHeaderView
class JYChargebackStatueTableHeaderView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    ///當做tableHeaderView 下面這樣設置 topAnchor
        override func didMoveToSuperview() {
            super.didMoveToSuperview()
            if let superV = self.superview{
                self.topAnchor.constraint(equalTo: superV.topAnchor, constant: 0).isActive = true
                self.leadingAnchor.constraint(equalTo: superV.leadingAnchor, constant: 0).isActive = true
                self.widthAnchor.constraint(equalTo: superV.widthAnchor, constant: 0).isActive = true
                self.trailingAnchor.constraint(equalTo: superV.trailingAnchor).isActive = true
            }
        }
    
//    ///作為tableFooterView 設置下面的方法 bottomAnchor
//    override func didMoveToSuperview() {
//        super.didMoveToSuperview()
//        if let superV = self.superview{
//            self.bottomAnchor.constraint(equalTo: superV.bottomAnchor, constant: 0).isActive = true
//            self.leadingAnchor.constraint(equalTo: superV.leadingAnchor, constant: 0).isActive = true
//            self.widthAnchor.constraint(equalTo: superV.widthAnchor, constant: 0).isActive = true
//            self.trailingAnchor.constraint(equalTo: superV.trailingAnchor).isActive = true
//        }
//    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

// MARK: - UI
extension JYChargebackStatueTableHeaderView{
    
    private func configUI(){
///改為false 可以使用VFL 布局 或者layout布局
        self.translatesAutoresizingMaskIntoConstraints = false
    }
}

 

4.設置TableFooterView

// ==================== 分 ==================== 割 ==================== 線 ====================
///退款狀態控制器的TableFooterView:查看完整協商記錄
class JYChargebackStatueTableFooterView: UIView {
    var clickHandle:(()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
//    ///當做tableHeaderView 下面這樣設置 topAnchor
//    override func didMoveToSuperview() {
//        super.didMoveToSuperview()
//        if let superV = self.superview{
//            self.topAnchor.constraint(equalTo: superV.topAnchor, constant: 0).isActive = true
//            self.leadingAnchor.constraint(equalTo: superV.leadingAnchor, constant: 0).isActive = true
//            self.widthAnchor.constraint(equalTo: superV.widthAnchor, constant: 0).isActive = true
//            self.trailingAnchor.constraint(equalTo: superV.trailingAnchor).isActive = true
//        }
//    }
    
    ///作為tableFooterView 設置下面的方法 bottomAnchor
    override func didMoveToSuperview() {
        super.didMoveToSuperview()
        if let superV = self.superview{
            self.bottomAnchor.constraint(equalTo: superV.bottomAnchor, constant: 0).isActive = true
            self.leadingAnchor.constraint(equalTo: superV.leadingAnchor, constant: 0).isActive = true
            self.widthAnchor.constraint(equalTo: superV.widthAnchor, constant: 0).isActive = true
            self.trailingAnchor.constraint(equalTo: superV.trailingAnchor).isActive = true
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func clickEvent(){
        clickHandle?()
    }
}

// MARK: - UI
extension JYChargebackStatueTableFooterView{
    private func configUI(){
        self.translatesAutoresizingMaskIntoConstraints = false
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(clickEvent)))
    }
}

 

5. VC之中使用

    
private lazy var tableHeaderView = JYChargebackStatueTableHeaderView()

private lazy var tableFooterView : JYChargebackStatueTableFooterView = {
        let tableFooterView = JYChargebackStatueTableFooterView()
        tableFooterView.clickHandle = {[weak self] in
            if let thisSelf = self{
                thisSelf.navigationController?.pushViewController(JYNegotiationHistoryController(), animated: true)
            }
        }
        return tableFooterView
    }()


    /// tv的cell注冊ID
    private lazy var cellId = "JYChargebackStatueCell"
    fileprivate lazy var tv : UITableView = {
        /// 創建一個分組TV
        let tv = UITableView(frame: CGRect.zero, style: UITableView.Style.plain)
        tv.translatesAutoresizingMaskIntoConstraints = false
        tv.separatorStyle = .none
        tv.delegate = self
        tv.dataSource = self
        tv.bounces = false
        tv.register(JYChargebackStatueCell.self, forCellReuseIdentifier: cellId)
        tv.tableHeaderView = tableHeaderView
        tv.tableFooterView = tableFooterView
        
        return tv
    }()



  ///在控制器中layoutSubViews 方法設置
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        ///設置tableHeaderView 的size
        tableHeaderView.frame.size = CGSize(width: JYScreenWidth, height: tableHeaderView.jy.getLayoutSize().height)

        ///設置tableFooterView的size
        tableFooterView.frame.size = CGSize(width: JYScreenWidth, height: tableFooterView.jy.getLayoutSize().height)
        ///tableFooterView 的 translatesAutoresizingMaskIntoConstraints 要改為false
        tableFooterView.translatesAutoresizingMaskIntoConstraints = true
    }

 

如果在自定義view中使用的話

    override func layoutSubviews() {
        super.layoutSubviews()
                ///設置tableHeaderView 的size
        tableHeaderView.frame.size = CGSize(width: JYScreenWidth, height: tableHeaderView.jy.getLayoutSize().height)

        ///設置tableFooterView的size
        tableFooterView.frame.size = CGSize(width: JYScreenWidth, height: tableFooterView.jy.getLayoutSize().height)
        ///tableFooterView 的 translatesAutoresizingMaskIntoConstraints 要改為false
        tableFooterView.translatesAutoresizingMaskIntoConstraints = true
    }

 

 

 

 

// MARK: - UIView操作API
extension JYBaseExtension where Base: UIView{
    
    /// 獲得一個VFL 或者 layout的控件的size
    func getLayoutSize() -> CGSize{
        self.base.setNeedsLayout()
        // 立馬布局子視圖
        self.base.layoutIfNeeded()
        return self.base.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM