RSMasterTableViewKit
为什么使用 RSMasterTableViewKit?
UITableView 是 iOS 应用的最常用 UIKit 控件。仅使用 UITableview 无法完成任何屏幕的功能。基于功能,开发者还需要编写代码以实现 PullToRefresh、搜索栏、分页、显示加载指示器、空数据集以及更多其他复杂场景。作为开发者,您不觉得每次都重复编写这些代码很累吗?
RSMasterTableViewKit 旨在帮助开发者以最小的编码工作快速将 UITableView 与他们想要的定制功能集成。
特性
- 易于使用的数据源实现(无需编写 cellForRowAtIndexPath)
- 下拉刷新
- 分页
- 搜索栏(本地 & 网页搜索)
- 空数据集
- 带标题的加载指示器
- JSON 到 Codable 转换
- 网络(WebAPI 请求:GET、POST 等)
- 网络可达性检查
需求
iOS 10.0+ | Xcode 8.3+ | Swift 4.0+
安装
CocoaPods
pod 'RSMasterTableViewKit' or pod 'RSMasterTableViewKit', '~> 1.2'
用法
通用数据源
// connect UITableview outlet from storyboard
@IBOutlet weak var tableView: RSTableView!
// declare a dataSource
var dataSource: RSTableViewDataSource<Comment>? //here Comment is the datamodel
// setup tableview
dataSource = RSTableViewDataSource<Comment>(tableView: tableView, identifier: "cell") { (cell, comment, indexPath) in
cell.textLabel?.text = "\(indexPath.row+1). \(comment.email)"
cell.detailTextLabel?.text = comment.body
}
空数据集
// set image, title, description
tableView.setEmptyDataView(title: NSAttributedString(string: "NO COMMENTS AVAILABLE"), description: NSAttributedString(string: "Comments that you've posted will appear here."), image: UIImage(named: "nodata-comments"), background: nil)
// or
// customize background color
tableView.setEmptyDataView(title: NSAttributedString(string: "No Data found"), description: nil, image: nil, background: RSEmptyDataBackground.color(color: UIColor.white))
// or
// background view or image
tableView.setEmptyDataView(title: nil, description: NSAttributedString(string: "No Results"), image: nil, background: RSEmptyDataBackground.view(view: imageView))
下拉刷新
tableView.addPullToRefresh { [weak self] in
// your code to handle pull to refresh action
DispatchQueue.global().asyncAfter(deadline: .now()+2, execute: {
// refresh data
})
}
// customize title and color
tableView.setPullToRefresh(tintColor: UIColor.darkGray, attributedText: NSAttributedString(string: "Fetching data"))
分页
tableView.addPagination { (page) in
// url for next page
let url = self?.getURLForPage(page)
// fetch & append data
self?.fetchDataFromServer(url: url!, completion: { (list) in
self?.dataSource?.appendData(data: list)
})
}
// set pagination parameters (Default: Start = 0 & Size = 20)
let paginationParams = PaginationParameters(page: 1, size: 50)
tableView.addPagination(parameters: paginationParams) { [weak self] (page) in
}
搜索框
tableView.addSearchBar()
// or
tableView.addSearchBar(placeHolder: "Search..", noResultMessage: NSAttributedString(string: "No result matching your search criteria"))
// search handler
dataSource?.searchResultHandler = { [weak self] (searchString, dataArray) in
// filter
let result = dataArray.filter({ $0.email.starts(with: searchString) })
self?.dataSource?.setSearchResultData(result, replace: false)
// Note:
// replace: true - will replace your main dataSource (use in case you want to search data from web)
// replace: false - will maintain your main dataSource (use in case you want to search within existing data)
}
加载指示器
tableView.showIndicator(title: NSAttributedString(string: "LOADING"), tintColor: UIColor.darkGray)
网络
// prepare request -> This returns array of comments
let request = DataModelRequest<[Comment]>(url: "URL Here")
// execute request
request.execute(success: { [weak self] (comments) in
self?.dataSource?.appendData(data: comments)
}) { [weak self] (error) in
self?.tableView.hideAllAnimations()
}
// or
// JSON Request
let jsonrequest = JSONRequest(url: "", method: .POST, headers: nil, parameters: nil, responeKeyPath: "data")
// Note: Specify keypath here, if you want only key specific json data
// "data" or "data.contents" etc.
数据源操作
// append rows to datasource
self.dataSource?.appendData(data: list)
// append at top
self.dataSource?.prependData(data: list)
// set or replace existing data with new
self.dataSource?.setData(data: list)
// clear
self.dataSource?.clearData()
网络可达性
if ReachabilityManager.isReachable {
// your code
}
// handler reachability changes
NotificationCenter.default.addObserver(self, selector: #selector(handleReachabiltyChanges(notification:)), name: NSNotification.Name.init(reachabilityChangedNotification), object: nil)
@objc func handleReachabiltyChanges(notification: Notification) {
let isReachable = notification.userInfo?[reachabilityStatus] as? Bool ?? false
if isReachable {
// inform user
}
}
示例
更多详细信息,请参阅示例。
许可
RSMasterTableViewKit 以 MIT 许可证发布。有关详细信息,请参阅LICENSE。