项目中封装tableView占位图在OC时代已经很多,但Swift方面查了网上很多方法,都比较模糊,主要问题在于swift4.0后不允许调用load()以及重写initialize()方法了。这篇文章主要为了记录一下自己综合各方面的文章得出的结果。
感谢Jordan Smith大神在之前提供的实现load()的方法
首先,我们需要解决在应用开始时加载方法交换的问题
给UIApplication声明extension,将方法体以单例的方式实现
extension UIApplication {
static let runOnce: Void = {
ChangePointer.changePointerFunction()
}()
}
其次,声明一个协议,用来做方法交换
protocol MethodSwizzling : class {
static func awake()
static func swizzlingForClass(_ forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector)
}
extension MethodSwizzling {
static func swizzlingForClass(_ forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {
let originalMethod = class_getInstanceMethod(forClass, originalSelector)
let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector)
guard (originalMethod != nil && swizzledMethod != nil) else {
return
}
if class_addMethod(forClass, originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!)) {
class_replaceMethod(forClass, swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
} else {
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
}
}
然后是方法交换的调用方法,也就是给我们开始写给UIApplication的extension调用的方法实现
class ChangePointer {
static func changePointerFunction() {
let typeCount = Int(objc_getClassList(nil, 0))
let types = UnsafeMutablePointer<AnyClass>.allocate(capacity: typeCount)
let autoreleasingTypes = AutoreleasingUnsafeMutablePointer<AnyClass>(types)
objc_getClassList(autoreleasingTypes, Int32(typeCount))
for index in 0 ..< typeCount {
(types[index] as? MethodSwizzling.Type)?.awake()
}
types.deallocate()
}
}
Jordan Smith当时使用的是UIResponder的next这个计算属性来实现调用runOnce,但我试了很久,发现didFinishLaunchingWithOptions并不执行next属性,原因不明,索性直接在didFinishLaunchingWithOptions里主动调用runOnce方法。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.runOnce //调用方法交换
if #available(iOS 13, *) {
return true
} else {
//正常的启动流程
return true
}
}
接下来,就是tableView的reloadData的替换了,这部分跟以前OC时区别不大,毕竟有了MethodSwizzling,搞什么都可以
我是声明了两个全局变量,其实isEmpty这个放不放成全局变量都行
private var empty_placeHolder_tag: UInt8 = 22 //
private var is_empty_tag: UInt8 = 33
extension UITableView: MethodSwizzling {
static func awake() { //实现协议声明的awake方法,调用交换reloadData方法
swizzleMethod
}
private static let swizzleMethod: Void = {
let originalSelector = #selector(reloadData)
let swizzledSelector = #selector(empty_reloadData)
swizzlingForClass(UITableView.self, originalSelector: originalSelector, swizzledSelector: swizzledSelector)
}()
@objc func empty_reloadData() {
self.checkEmpty()
empty_reloadData()
}
//声明placeHolderView,这里的RBaseHolderView是我自定义的一个View,无非就是图片,文字,非常简单
var placeHolderView : RBasePlaceHolderView? {
set {
objc_setAssociatedObject(self, &empty_placeHolder_tag, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
return (objc_getAssociatedObject(self, &empty_placeHolder_tag) as? RBasePlaceHolderView)
}
}
// 这个是判断当前TableView是否有数据,可以声明为局部变量,我是为了在另一个自己做的TableView上,做上拉加载和下拉刷新判断搞出来的
var isEmpty : Bool {
set {
objc_setAssociatedObject(self, &is_empty_tag, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
get {
return (objc_getAssociatedObject(self, &is_empty_tag) as! Bool)
}
}
// 判断数据源,也就是占位图是否显示的方法
func checkEmpty() {
isEmpty = true //可以在这里将isEmpty改为局部变量,
let changeDataSource = self.dataSource
var sections : Int?
sections = changeDataSource?.numberOfSections?(in: self)
if sections != nil {
for index in 0 ..< sections! {
if let rows = changeDataSource?.tableView(self, numberOfRowsInSection: index) {
if rows > 0 {
isEmpty = false
break
}
}
}
}
if isEmpty == true {
self.placeHolderView?.removeFromSuperview()
if self.placeHolderView != nil {
self.addSubview(self.placeHolderView!)
}
}
else{
self.placeHolderView?.removeFromSuperview()
}
}
}
流程到这里就结束了,在外部调用的话,只需要在外部VC的viewDidLoad这样赋给placeHolderView一个任意的View即可
self.refreshTableView.placeHolderView = RBasePlaceHolderView(holderText: "测试一下")
2020.7.1更新
发现这种方法会导致启动速度拖慢1到2秒,原因在didFinishLaunch时调用的runOnce
static let runOnce: Void = {
ChangePointer.changePointerFunction()
}()
ChangePointer.changePointerFunction()这个方法内部做了遍历,将所有系统的类库都遍历了一遍,25000多个类啊。但对于我们来说,仅仅为了TableView的替换,所以,我们这么修改一下
static let runOnce: Void = {
UITableView.awake()
}()
只做我们需要的类的替换,这样就OK了
网友评论