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
public protocol BSTableViewProtocol { }
publicextensionBSTableViewProtocol {
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
}
publicfunc 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
}
}
extensionUIViewController {
private struct BSVCKey {
staticvar sKey = "bs_scrollViewKey"
static var oKey = "bs_upOffsetKey"
}
@objc public var bs_scrollView: UIScrollView? {
get { returnobjc_getAssociatedObject(self, &BSVCKey.sKey) as? UIScrollView }
set { objc_setAssociatedObject(self, &BSVCKey.sKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
}
public var bs_upOffset: String? {
get { returnobjc_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.
}
}
extensionReceiptManagerVC: 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)行")
}
}