之前在学习swift时写过一个计划类的Demo,比较简陋,前些天发现了一个很有趣的第三方库DZNEmptyDataSet,它可以让你的app在列表为空的时候显示一些其他的信息而不是单纯地空白,就像下面这样:
![](https://img.haomeiwen.com/i1830151/926f6dcbeb2206bc.png)
是不是很炫酷?瞬间感觉高大上了许多有木有?
但是问题来了,这个库是用oc写的,所以不能直接在swift的项目中使用,需要简单的几个步骤才可以提升我们app的逼格,下面我们实际操作一下。
首先我们需要一个简单的Demo来实现一个TableView,新建一个单页面项目,命名为DZNSample,接下来删掉storyboard中的ViewController,从控件库中拖一个TableViewController进来,然后新建一个swift文件,继承自UITableViewController,命名为DZNTableViewController.swift,再回到storyboard中,改变之前拖进来的TableViewController继承的类为我们新建的DZNTableViewController,同时将这个View设为app启动后的第一个View,然后将Cell的Identifier设为DZNCell,全部搞定了大概像这样:
![](https://img.haomeiwen.com/i1830151/29ad5f652a41d642.png)
接下来就是在DZNTableViewController中写代码了,为了简单,这个Demo初始的Row设为0,代码如下:
import UIKit
class DZNTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DZNCell", for: indexPath)
return cell
}
}
我们运行一下:
![](https://img.haomeiwen.com/i1830151/712e3ce98b1e4250.png)
什么都没有,确实是这样,但是很丑,所以接下来我们就要用DZNEmptyDataSet让它变漂亮。
首先需要在项目中导入DZNEmptyDataSet这个库,我个人推荐使用CocoaPods,方便快捷,不会使用的小伙伴可以看下这两篇文章,说的很详细:2016最新cocoapods安装遇到的问题及解决方法,CocoaPods安装、超详细。
CocoaPods安装好后我们要从终端进入到项目的根目录,输入touch Podfile后回车,我们就在项目目录中建立了一个Podfile文件,然后打开这个Podfile文件,输入:
target 'DZNSample' do
pod 'DZNEmptyDataSet', '~> 1.8'
end
保存后关闭这个文件,在终端中输入pod install,稍等一会儿就装好啦。装好了差不多是这样的:
![](https://img.haomeiwen.com/i1830151/4d121ba3b67d5c07.png)
OK,现在我们再次打开我们的项目,注意,现在打开的是以 .xcworkspace为后缀的文件。
新建一个头文件,命名为Header就可以,头文件中的代码如下:
#ifndef Header_h
#define Header_h
#import "UIScrollView+EmptyDataSet.h"
#endif /* Header_h */
然后需要建立一个桥接,在Build Settings中找到Bridging Header,将我们刚才建立的Header拖进去,或者手动输入Header的地址。
接下来打开DZNTableViewController,遵循这两个协议:
DZNEmptyDataSetSource
DZNEmptyDataSetDelegate
更改viewDidLoad的代码:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.emptyDataSetSource = self
self.tableView.emptyDataSetDelegate = self
self.tableView.tableFooterView = UIView() //去掉表格视图中多余的线
}
最后实现协议中的方法:
//实现第三方库协议的方法
func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
let text = "现在开始,计划你的第一步"
let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(18.0)), NSForegroundColorAttributeName: UIColor.darkGray]
return NSAttributedString(string: text, attributes: attributes)
}
func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
let text = "这里列举了你在这个类别下的所有计划"
let paragraph = NSMutableParagraphStyle()
paragraph.lineBreakMode = .byWordWrapping
paragraph.alignment = .center
let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: CGFloat(14.0)), NSForegroundColorAttributeName: UIColor.lightGray, NSParagraphStyleAttributeName: paragraph]
return NSAttributedString(string: text, attributes: attributes)
}
func buttonTitle(forEmptyDataSet scrollView: UIScrollView!, for state: UIControlState) -> NSAttributedString! {
let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(17.0))]
return NSAttributedString(string: "Continue", attributes: attributes)
}
func backgroundColor(forEmptyDataSet scrollView: UIScrollView!) -> UIColor! {
return UIColor.white
}
func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
return true
}
func emptyDataSetShouldAllowTouch(_ scrollView: UIScrollView!) -> Bool {
return true
}
func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
return false
}
func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView!) -> Bool {
return false
}
最后最后不要忘了更新视图(在这个Demo中不需要更新):
tableView.reloadData()
完整代码如下:
import UIKit
class DZNTableViewController: UITableViewController,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.emptyDataSetSource = self
self.tableView.emptyDataSetDelegate = self
self.tableView.tableFooterView = UIView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DZNCell", for: indexPath)
return cell
}
//实现第三方库协议的方法
func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
let text = "现在开始,计划你的第一步"
let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(18.0)), NSForegroundColorAttributeName: UIColor.darkGray]
return NSAttributedString(string: text, attributes: attributes)
}
func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
let text = "这里列举了你在这个类别下的所有计划"
let paragraph = NSMutableParagraphStyle()
paragraph.lineBreakMode = .byWordWrapping
paragraph.alignment = .center
let attributes = [NSFontAttributeName: UIFont.systemFont(ofSize: CGFloat(14.0)), NSForegroundColorAttributeName: UIColor.lightGray, NSParagraphStyleAttributeName: paragraph]
return NSAttributedString(string: text, attributes: attributes)
}
func buttonTitle(forEmptyDataSet scrollView: UIScrollView!, for state: UIControlState) -> NSAttributedString! {
let attributes = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: CGFloat(17.0))]
return NSAttributedString(string: "Continue", attributes: attributes)
}
func backgroundColor(forEmptyDataSet scrollView: UIScrollView!) -> UIColor! {
return UIColor.white
}
func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
return true
}
func emptyDataSetShouldAllowTouch(_ scrollView: UIScrollView!) -> Bool {
return true
}
func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
return false
}
func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView!) -> Bool {
return false
}
}
大功告成,现在运行一下:
![](https://img.haomeiwen.com/i1830151/280d5e6b0b6189b9.png)
是不是瞬间好看了很多?
网友评论