AZRemoteTable
加载数据从未如此简单
功能
- 自动加载更多内容,当滚动时
🚀 - 支持下拉刷新
🏓 - 与任何UITableView一起工作!
🎯 - 错误处理
🕸 - 自定义视图和子类化
🎨
安装
pod 'AZRemoteTable'
或只需将源
文件夹拖放到您的项目中。
用法
第 #1 步:创建数据源
class CustomRemoteDataSource: AZRemoteTableDataSource {
//An array in which we will store the data.
fileprivate var items: [String] = []
/// Helper function to add items.
public func addItems(items: [String]){
for item in items { self.items.append(item) }
}
/// Helper function to clear items.
public func clearItems() {
items.removeAll()
reset()
}
// MARK: - AZRemoteTableDataSource
override func cellForRowAt(_ tableView: UITableView, indexPath: IndexPath) -> UITableViewCell {
//simple implementation of cellForRowAt:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = items[indexPath.row]
return cell
}
override func numberOfRowsInSection(_ tableView: UITableView, section: Int) -> Int {
//return the number of items currently held.
return items.count
}
}
第 #2 步:创建代理人
class CustomRemoteDelegate: AZRemoteTableDelegate {
override func tableView(_ tableView: UITableView,
didRequestPage page: Int,
usingRefreshControl: Bool) {
// Make an API call.
APIManager.shared.loadData(id: "a9dd9e00-55f8-4ce8-8ada-8e933d052e3a",
token: "b54695d5-6f98-442f-9b39-4509fcd8aacd",
page: page,
itemsPerPage: 30) { (message, data, error) in
//check for errors
if error {
//error found, notify table
tableView.remote.notifyError()
}else {
//all good, data fetched, add it to the data source.
let dataSource = tableView.remote.dataSource as? CustomRemoteDataSource
//clear all items if we used pull to refresh
if usingRefreshControl { dataSource?.clearItems() }
//update data source
dataSource?.addItems(items: data)
//notify table that we are done, and if we have more data to load.
tableView.remote.notifySuccess(hasMore: true)
}
}
}
}
第 #3 步:在您的控制器中使用它
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let delegate = CustomRemoteDelegate()
let dataSource = CustomRemoteDataSource()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = delegate
tableView.dataSource = dataSource
tableView.remote.initialLoad()
}
}