zoukankan      html  css  js  c++  java
  • 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( JYScreenWidth, height: tableHeaderView.jy.getLayoutSize().height)
    
            ///设置tableFooterView的size
            tableFooterView.frame.size = CGSize( JYScreenWidth, height: tableFooterView.jy.getLayoutSize().height)
            ///tableFooterView 的 translatesAutoresizingMaskIntoConstraints 要改为false
            tableFooterView.translatesAutoresizingMaskIntoConstraints = true
        }

    如果在自定义view中使用的话

        override func layoutSubviews() {
            super.layoutSubviews()
                    ///设置tableHeaderView 的size
            tableHeaderView.frame.size = CGSize( JYScreenWidth, height: tableHeaderView.jy.getLayoutSize().height)
    
            ///设置tableFooterView的size
            tableFooterView.frame.size = CGSize( 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)
        }
    }
  • 相关阅读:
    Python-类和实例
    Python之操作文件和目录
    Python之split()函数
    Python之切片操作
    PyCharm导入selenium的webdirver模块出错
    Python编写“去除字符串中所有空格”
    Python编写“求一元二次方程的解”
    android开发学习 ------- 【转】Genymotion 小白安装
    android开发学习 ------- git
    android开发学习 ------- MongoDB数据库简单理解
  • 原文地址:https://www.cnblogs.com/qingzZ/p/13522772.html
Copyright © 2011-2022 走看看