iOS - Swift - TableView、CollectionView 空白佔點陣圖 EmptyDataSet
GitHub: EmptyDataSet-Swift
當 UITableView/UICollectionView 資料為空時顯示佔點陣圖,DZNEmptyDataSet 的 Swift 版本。
ScreenShot
Usage
Basic
基本用法和 DZNEmptyDataSet 一樣
import EmptyDataSet_Swift
class OriginalUsageViewController: UITableViewController, EmptyDataSetSource, EmptyDataSetDelegate {
override func viewDidLoad() {
super.viewDidLoad()
tableView.emptyDataSetSource = self
tableView.emptyDataSetDelegate = self
}
}
EmptyDataSetDelegate
public protocol EmptyDataSetDelegate: class {
/// Asks the delegate to know if the empty dataset should fade in when displayed. Default is true.
func emptyDataSetShouldFadeIn(_ scrollView: UIScrollView) -> Bool
/// Asks the delegate to know if the empty dataset should still be displayed when the amount of items is more than 0. Default is false.
func emptyDataSetShouldBeForcedToDisplay(_ scrollView: UIScrollView) -> Bool
/// Asks the delegate to know if the empty dataset should be rendered and displayed. Default is true.
func emptyDataSetShouldDisplay(_ scrollView: UIScrollView) -> Bool
/// Asks the delegate for touch permission. Default is true.
func emptyDataSetShouldAllowTouch(_ scrollView: UIScrollView) -> Bool
/// Asks the delegate for scroll permission. Default is false.
func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool
/// Asks the delegate for image view animation permission. Default is false.
/// Make sure to return a valid CAAnimation object from imageAnimationForEmptyDataSet:
func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView) -> Bool
/// Tells the delegate that the empty dataset view was tapped.
/// Use this method either to resignFirstResponder of a textfield or searchBar.
func emptyDataSet(_ scrollView: UIScrollView, didTapView view: UIView)
/// Tells the delegate that the action button was tapped.
func emptyDataSet(_ scrollView: UIScrollView, didTapButton button: UIButton)
/// Tells the delegate that the empty data set will appear.
func emptyDataSetWillAppear(_ scrollView: UIScrollView)
/// Tells the delegate that the empty data set did appear.
func emptyDataSetDidAppear(_ scrollView: UIScrollView)
/// Tells the delegate that the empty data set will disappear.
func emptyDataSetWillDisappear(_ scrollView: UIScrollView)
/// Tells the delegate that the empty data set did disappear.
func emptyDataSetDidDisappear(_ scrollView: UIScrollView)
}
EmptyDataSetSource
public protocol EmptyDataSetSource: class {
/// Asks the data source for the title of the dataset.
/// The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.
func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString?
/// Asks the data source for the description of the dataset.
/// The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.
func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString?
/// Asks the data source for the image of the dataset.
func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage?
/// Asks the data source for a tint color of the image dataset. Default is nil.
func imagetintColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor?
/// Asks the data source for the image animation of the dataset.
func imageAnimation(forEmptyDataSet scrollView: UIScrollView) -> CAAnimation?
/// Asks the data source for the title to be used for the specified button state.
/// The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.
func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControlState) -> NSAttributedString?
/// Asks the data source for the image to be used for the specified button state.
/// This method will override buttonTitleForEmptyDataSet:forState: and present the image only without any text.
func buttonImage(forEmptyDataSet scrollView: UIScrollView, for state: UIControlState) -> UIImage?
/// Asks the data source for a background image to be used for the specified button state.
/// There is no default style for this call.
func buttonBackgroundImage(forEmptyDataSet scrollView: UIScrollView, for state: UIControlState) -> UIImage?
/// Asks the data source for the background color of the dataset. Default is clear color.
func backgroundColor(forEmptyDataSet scrollView: UIScrollView) -> UIColor?
/// Asks the data source for a custom view to be displayed instead of the default views such as labels, imageview and button. Default is nil.
/// Use this method to show an activity view indicator for loading feedback, or for complete custom empty data set.
/// Returning a custom view will ignore -offsetForEmptyDataSet and -spaceHeightForEmptyDataSet configurations.
func customView(forEmptyDataSet scrollView: UIScrollView) -> UIView?
/// Asks the data source for a offset for vertical alignment of the content. Default is 0.
func verticalOffset(forEmptyDataSet scrollView: UIScrollView) -> CGFloat
/// Asks the data source for a vertical space between elements. Default is 11 pts.
func spaceHeight(forEmptyDataSet scrollView: UIScrollView) -> CGFloat
}
Extensions
除了 DZNEmptyDataSet 中介紹的用法,另外擴充了不需要通過實現協議 DZNEmptyDataSetSource 和 DZNEmptyDataSetDelegate 的用法。
只要給需要的 tableView 呼叫下面方法
public func emptyDataSetView(_ closure: @escaping (EmptyDataSetView) -> Void)
像這樣子使用
tableView.emptyDataSetView { view in
view.titleLabelString(titleString)
.detailLabelString(detailString)
.image(image)
.imageAnimation(imageAnimation)
.buttonTitle(buttonTitle, for: .normal)
.buttonTitle(buttonTitle, for: .highlighted)
.buttonBackgroundImage(buttonBackgroundImage, for: .normal)
.buttonBackgroundImage(buttonBackgroundImage, for: .highlighted)
.dataSetBackgroundColor(backgroundColor)
.verticalOffset(verticalOffset)
.verticalSpace(spaceHeight)
.shouldDisplay(true, view: tableView)
.shouldFadeIn(true)
.isTouchAllowed(true)
.isScrollAllowed(true)
.isImageViewAnimateAllowed(isLoading)
.didTapDataButton {
// Do something
}
.didTapContentView {
// Do something
}
}
CustomView
注意: 通過 EmptyDataSetSource 設定了 CustomView 其他設定都會無效。通過鏈式方式設定 CustomView 其他控制元件的自動佈局會無效。
func customView(forEmptyDataSet scrollView: UIScrollView) -> UIView? {
let view = UIView()
view.frame = CGRect.init(x: 0, y: 100, width: UIScreen.main.bounds.width, height: 100)
view.backgroundColor = UIColor.lightGray
let label = UILabel()
label.frame = CGRect.init(x: 0, y: 10, width: UIScreen.main.bounds.width, height: 40)
label.text = "Test CustomView"
view.addSubview(label)
return view
}
上述程式碼顯示效果如下:
相關文章
- iOS自定義控制元件:自定義TableView、CollectionView空資料佔點陣圖iOS控制元件View
- iOS - 二級連動(tableview包含 collectionview)iOSView
- 如何給TableView、CollectionView新增動效View
- 更新TableView和CollectionView的狀態View
- [iOS]一行程式碼整合空白頁面佔點陣圖(基於runtime+MJRefresh思想)iOS行程
- 代理模式+react+ 圖片佔點陣圖模式React
- Swift tableView基本使用SwiftView
- 『ios』view和tableview的截圖和圖片拼接iOSView
- iOS CollectionView 的那些事iOSView
- layui圖片懶載入-loading佔點陣圖UI
- Swift多執行緒之Operation:非同步載入CollectionView圖片Swift執行緒非同步View
- UITableView佔點陣圖的低耦合性設計UIView
- Splash 佔點陣圖適配 全面屏筆記筆記
- iOS 中tableview cell點選取消選中效果iOSView
- Swift iOS : 如何一拖tableview到底的時候更新資料SwiftiOSView
- 點陣圖
- ios的collectionView區頭設定iOSView
- 成品直播原始碼推薦,TableView/CollectionView 滑動頂部效果最佳化原始碼View
- Swift 專案總結 03 自定義 CollectionView 佈局SwiftView
- 使用點陣圖選單項——建立點陣圖 (轉)
- 點陣圖排序排序
- iOS tableView中的MVC、MVVMiOSViewMVCMVVM
- 線上生成佔點陣圖片工具:簡便快捷的設計利器
- html 空白漢字佔位符HTML
- iOS 常用圖片格式判斷 (Swift)iOSSwift
- iOS專案開發實戰——Swift實現多個TableView的側滑與切換iOSSwiftView
- iOS 0行程式碼實現 TableView 無資料時展示佔位檢視iOS行程View
- iOS面向切面的TableView-AOPTableViewiOSView
- iOS cell找對應的tableViewiOSView
- iOS開發- tableView的協議iOSView協議
- 微信小程式image載入成功前顯示預設佔點陣圖微信小程式
- 零行程式碼為 App 新增異常載入佔點陣圖行程APP
- DDGScreenShot —iOS 圖片處理--多圖片拼接 (swift)iOSSwift
- 點陣圖索引.sql索引SQL
- iOS下點選任意空白Dismiss當前自定義的AlertViewiOSView
- 點陣圖索引(Bitmap Index)——從B*樹索引到點陣圖索引索引Index
- Swift iOS : WebView快取圖片的方法SwiftiOSWebView快取
- iOS Swift 仿微信聊天圖片顯示iOSSwift