雷火电竞-中国电竞赛事及体育赛事平台

歡迎來(lái)到入門(mén)教程網(wǎng)!

Swift

當(dāng)前位置:主頁(yè) > 軟件編程 > Swift >

Swift解決UITableView空數(shù)據(jù)視圖問(wèn)題的簡(jiǎn)單方法

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-11|欄目:Swift|點(diǎn)擊:

前言

UITableView在現(xiàn)如今的APP中已經(jīng)成為必不可少的一個(gè)控件,所以今天給大家?guī)?lái)UITableView在Swift中是如何實(shí)現(xiàn)的,下面這篇文章主要給大家介紹了關(guān)于Swift解決UITableView空數(shù)據(jù)視圖的相關(guān)內(nèi)容,下面話(huà)不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧

tableView空數(shù)據(jù)問(wèn)題

一般項(xiàng)目中tableView若數(shù)據(jù)為空時(shí)會(huì)有一個(gè)提示示意圖

為了更好的管理這種提示示意圖,筆者利用extension進(jìn)行了簡(jiǎn)單的拓展

解決思路

利用swift面向協(xié)議的特點(diǎn),使用協(xié)議來(lái)進(jìn)行設(shè)置。

  • 設(shè)計(jì)空視圖協(xié)議
  • tableView設(shè)置空視圖代理
  • 每次重繪tableView時(shí)判斷添加或移除空數(shù)據(jù)提示圖


具體實(shí)現(xiàn)

空視圖協(xié)議,遵守協(xié)議必須實(shí)現(xiàn)showEmtpy屬性

private let EmptyViewTag = 12345;

protocol EmptyViewProtocol: NSObjectProtocol {
 
 ///用以判斷是會(huì)否顯示空視圖
 var showEmtpy: Bool {get}
 
 ///配置空數(shù)據(jù)提示圖用于展示
 func configEmptyView() -> UIView?
}

extension EmptyViewProtocol {
 
 func configEmptyView() -> UIView? {
  return nil
 }
}

tableView擴(kuò)展配置,實(shí)現(xiàn)空數(shù)據(jù)示意圖展示判斷

DispatchQueue.once和BQTool.exchangeMethod是只執(zhí)行一次方法交換操作,具體實(shí)現(xiàn)可看源碼

 func setEmtpyViewDelegate(target: EmptyViewProtocol) {
  self.emptyDelegate = target
  DispatchQueue.once(#function) {
   BQTool.exchangeMethod(cls: self.classForCoder, targetSel: #selector(self.layoutSubviews), newSel: #selector(self.re_layoutSubviews))
  }
 }

 @objc func re_layoutSubviews() {
  self.re_layoutSubviews()
  
  if self.emptyDelegate!.showEmtpy {
   
   guard let view = self.emptyDelegate?.configEmptyView() else {
    return;
   }
   
   view.tag = EmptyViewTag;
   self.addSubview(view)
   
  } else {
   
   guard let view = self.viewWithTag(EmptyViewTag) else {
    return;
   }
   view .removeFromSuperview()
  }
 }
 
//MARK:- ***** Associated Object *****
 private struct AssociatedKeys {
  static var emptyViewDelegate = "tableView_emptyViewDelegate"
 }
 
 private var emptyDelegate: EmptyViewProtocol? {
  get {
   return (objc_getAssociatedObject(self, &AssociatedKeys.emptyViewDelegate) as! EmptyViewProtocol)
  }
  set (newValue){
   objc_setAssociatedObject(self, &AssociatedKeys.emptyViewDelegate, newValue!, .OBJC_ASSOCIATION_RETAIN)
  }
 }

示例代碼

//關(guān)鍵部分代碼
class ViewController: UIViewController , EmptyViewProtocol {

 private var datas: Array<Dictionary<String, String>>?
 /// 空數(shù)據(jù)提示圖
 private var label: UILabel? 
 
 var showEmtpy: Bool {
  get {
   if let data = self.datas {
    return data.count == 0
   }
   return true
  }
 }
 
 override func viewDidLoad() {
  super.viewDidLoad()

  let tableView: UITableView = ...
  tableView.setEmtpyViewDelegate(target: self)
  self.view.addSubview(tableView)
 }
 
 func configEmptyView() -> UIView? {
 
  if let view = self.label {
   return view
  }
  
  let lab = UILabel(frame: CGRect(x: 100, y: 300, width: 200, height: 30))
  lab.text = "this is a test"
  lab.textAlignment = .center
  self.label = lab
  
  return lab
 }
}

效果圖如下

最后

  • 該設(shè)計(jì)較為簡(jiǎn)單方便管理,若有不妥之處望指出
  • 相關(guān)代碼請(qǐng)前往swiftCustomControl (本地下載)查看

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)我們的支持。

上一篇:Swift中添加雙擊手勢(shì)識(shí)別器

欄    目:Swift

下一篇:Swift使用CollectionView實(shí)現(xiàn)廣告欄滑動(dòng)效果

本文標(biāo)題:Swift解決UITableView空數(shù)據(jù)視圖問(wèn)題的簡(jiǎn)單方法

本文地址:http://www.jygsgssxh.com/a1/Swift/11923.html

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有