項(xiàng)目開(kāi)發(fā)中常需要站位圖施蜜,如tableview,但是項(xiàng)目開(kāi)發(fā)到中途体谒,可能有幾百個(gè)tableview在你到項(xiàng)目里杯聚。
這里我采用到辦法是寫(xiě)一個(gè)extension,實(shí)現(xiàn)一個(gè)類(lèi)方法抒痒。在appdelegate中調(diào)用這個(gè)類(lèi)方法實(shí)現(xiàn)幌绍。代碼如下:
extension UITableView {
/// 類(lèi)方法實(shí)現(xiàn)runtime的方法交換,處理tableview沒(méi)有數(shù)據(jù)時(shí)的背景圖
public class func initializeMethod() {
let originalSelector = #selector(UITableView.reloadData)
let swizzledSelector = #selector(jc_reloadData)
// runtime獲取函數(shù)方法
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
method_exchangeImplementations(originalMethod!, swizzledMethod!)
}
@objc func jc_reloadData(){
self.jc_reloadData()
let number = self.numberOfSections
var havingData = false
for i in 0..<number {
if self.numberOfRows(inSection: i) > 0{
havingData = true
break
}
}
if havingData {
self.backgroundView = UIImageView()
}else{
self.backgroundView = UIImageView.init(image: UIImage.init(named: "drawing_no_duty"))
self.backgroundView?.contentMode = .center
}
}
}
在appdelegate中調(diào)用
UITableView.initializeMethod()