目录
概述
WLEmptyState 是一个基于 iOS 的组件,可让您自定义 UITableView
或 UICollectionView
空数据集的消息。我们创建了一个带 WLEmptyState 组件的示例项目,以展示如何使用它。
运行示例项目
要运行 Example
项目
-
使用以下命令克隆仓库
git clone [email protected]:wizeline/WLEmptyState.git
-
移动到
Example
目录并运行以下命令pod install
安装 WLEmptyState
CocoaPods
WLEmptyState 可通过 CocoaPods 获得。要安装它,请将以下命令添加到您的 Podfile 中
pod 'WLEmptyState'
Carthage
Carthage 是一个去中心化的依赖管理器,它构建您的依赖并提供二进制框架。要使用 Carthage 将 WLEmptyState 集成到您的 Xcode 项目中,请在 Cartfile 中指定它
github "wizeline/WLEmptyState"
配置 WLEmptyState
WLEmptyState 组件使用了 方法交换。因此,为配置 WLEmptyState
,请按照以下步骤进行
-
在
AppDelegate
类中导入模块,添加import WLEmptyState
。 -
在
application(_ application:, didFinishLaunchingWithOptions:)
方法上调用静态方法configure()
。您的
AppDelegate
类应如下所示import WLEmptyState ... func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { WLEmptyState.configure() return true }
使用 WLEmptyState
配置完 WLEmptyState
后,您可以用它来为您的 UITableViewController
或 UICollectionViewController
提供视图。您需要遵守 WLEmptyStateDataSource
协议。例如,
class YourTableViewController: UITableViewController, WLEmptyStateDataSource {
override func viewDidLoad() {
super.viewDidLoad()
tableView.emptyStateDataSource = self
}
}
在运行项目并使用实体数据集为空时,您将能够看到默认的 WLEmptyState
视图。
默认图像
自定义默认 WLEmptyState
如果想要自定义文本、描述或图像,您需要实现 WLEmptyStateDataSource
函数。您可以在以下列表中找到自定义代码
- 自定义图像
func imageForEmptyDataSet() -> UIImage? {
return UIImage(named: "bubble_icon")
}
- 自定义标题
func titleForEmptyDataSet() -> NSAttributedString {
let title = NSAttributedString(string: "No messages", attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .headline)])
return title
}
- 自定义描述
func descriptionForEmptyDataSet() -> NSAttributedString {
let title = NSAttributedString(string: "There's no messages to show.", attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)])
return title
}
自定义图像
创建你自己的EmptyState
使用customViewForEmptyState()
允许你为Empty State提供自定义实现。
func customViewForEmptyState() -> UIView? {
let activityIndicatorView = UIActivityIndicatorView()
activityIndicatorView.startAnimating()
activityIndicatorView.color = .purple
return activityIndicatorView
}
为WLEmptyState做贡献
我们积极欢迎你的拉取请求。我们试图使对这个项目的贡献尽可能透明和易于访问,请阅读我们的贡献指南并遵循行为准则。如果你遇到任何代码问题,请打开GitHub上的问题。
贡献者名单。
许可
WLEmptyState遵循MIT许可协议。有关更多信息,请参阅LICENSE文件。