tableView自定制协议(已适配iPhone x)

自定义拓展实现:

//

//  BSTableViewProtocol.swift

//  PageJTotalProject

//

//  Created by goldrose on 2018/5/21.

//  Copyright © 2018年 goldrose. All rights reserved.

/*************<isTranslucent默认是true>********************

 特殊说明:导航栏isTranslucent的true与false会影响tableview底部是否下沉64-->true不会下沉64

 1,(代码实现)

 self.navigationController?.navigationBar.isTranslucent = true---> tableview不会下沉64

 self.navigationController?.navigationBar.isTranslucent = false---> tableview会下沉64

 2, (xib实现)

 isTranslucent = false, tableview、scrollview则top应设置为-->0;

 isTranslucent = true, tableview、scrollview则top应设置为-->64;

 3, 此处使用-->

 <1>isTranslucent = true

 let H: CGFloat = is_iphoneXBS ? (UIScreen.main.bounds.height - Y - 34 - 0) : (UIScreen.main.bounds.height - Y - 0)

 <2>isTranslucent = false

 let H: CGFloat = is_iphoneXBS ? (UIScreen.main.bounds.height - Y - 34 - 64) : (UIScreen.main.bounds.height - Y - 64)

 *****************<CloudClassroom中isTranslucent是false>****************/


import Foundation

import UIKit


public let is_iphoneXBS = (UIScreen.main.bounds.height == 812.0)

public protocol BSTableViewProtocol { }


public extension BSTableViewProtocol {

    

    private func configIdentifier(_ identifier: inout String) -> String {

        var index = identifier.index(of: ".")

        guard index != nil else { return identifier }

        index = identifier.index(index!, offsetBy: 1)

        identifier = String(identifier[index! ..< identifier.endIndex])

        return identifier

    }

    

    public func registerCell(_ tableView: UITableView, _ cellCls: AnyClass) {

        var identifier = NSStringFromClass(cellCls)

        identifier = configIdentifier(&identifier)

        tableView.register(cellCls, forCellReuseIdentifier: identifier)

    }

    

    public func cellWithTableView<T: UITableViewCell>(_ tableView: UITableView, cellStyle: UITableViewCellStyle = .default) -> T {

        var identifier = NSStringFromClass(T.self)

        identifier = configIdentifier(&identifier)

        var cell = tableView.dequeueReusableCell(withIdentifier: identifier)

        if cell == nil {

            cell = UITableViewCell(style: cellStyle, reuseIdentifier: identifier)

        }

        return cell as! T

    }

    

    

    

    public func tableViewConfig(_ delegate: UITableViewDelegate, _ dataSource: UITableViewDataSource, _ style: UITableViewStyle?) -> UITableView  {

        let Y: CGFloat = is_iphoneXBS ? 0 + 24.0 : 0

        let H: CGFloat = is_iphoneXBS ? (UIScreen.main.bounds.height - Y - 34) : (UIScreen.main.bounds.height - Y)

        let tableView = UITableView(frame:  CGRect(x: 0, y: Y, width: UIScreen.main.bounds.width, height: H), style: style ?? .plain)

        closeEstablished(tv: tableView)

        tableView.delegate = delegate

        tableView.dataSource = dataSource

        return tableView

    }

    

    public func tableViewConfig(_ frame: CGRect ,_ delegate: UITableViewDelegate, _ dataSource: UITableViewDataSource, _ style: UITableViewStyle?) -> UITableView  {

        let tableView = UITableView(frame: frame, style: style ?? .plain)

        closeEstablished(tv: tableView)

        tableView.delegate = delegate

        tableView.dataSource = dataSource

        return tableView

    }

    

    private func closeEstablished(tv: UITableView) {

        tv.estimatedRowHeight = 0

        tv.estimatedSectionHeaderHeight = 0

        tv.estimatedSectionFooterHeight = 0

    }

}


extension UIViewController {

    

    private struct BSVCKey {

        static var sKey = "bs_scrollViewKey"

        static var oKey = "bs_upOffsetKey"

    }

    

    @objc public var bs_scrollView: UIScrollView? {

        get { return objc_getAssociatedObject(self, &BSVCKey.sKey) as? UIScrollView }

        set { objc_setAssociatedObject(self, &BSVCKey.sKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }

    }

    

    public var bs_upOffset: String? {

        get { return objc_getAssociatedObject(self, &BSVCKey.oKey) as? String }

        set { objc_setAssociatedObject(self, &BSVCKey.oKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }

    }

}



实际使用:

import UIKit


class ReceiptManagerVC: UIViewController, BSTableViewProtocol {


    

    private lazy var tableView: UITableView = {

        print(UIScreen.main.bounds.height)

        let Y: CGFloat = is_iphoneXBS ? 0 + 24.0 : 0

        let H: CGFloat = is_iphoneXBS ? (view.bounds.height - Y - 34) : view.bounds.height - Y

        let tableView = tableViewConfig(CGRect(x: 0, y: Y, width: view.bounds.width, height: H), self, self, nil)

        tableView.separatorInset = UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)

        let headerV = UIView.init(frame: CGRect.init(x: 0, y: 0, width: screenWidth, height: 10))

        headerV.backgroundColor = RGBColor(242, green: 242, blue: 247, alpha: 1.0)

        tableView.tableHeaderView = headerV

        tableView.tableFooterView = UIView()

        return tableView

    }()

    

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        self.view.addSubview(tableView)

        bs_scrollView = tableView

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

    


}


extension ReceiptManagerVC: UITableViewDelegate, UITableViewDataSource {

    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return 3

    }

    

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return 50

    }

    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = cellWithTableView(tableView)

        cell.accessoryType = .disclosureIndicator

        cell.textLabel?.text = ["申请发票", "我的发票", "地址管理"][indexPath.row]

        return cell

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print("点击了第\(indexPath.row + 1)行")

    }

}




猜你喜欢

转载自blog.csdn.net/flyingfirefish/article/details/80423397