SRLoadMoreOnScrollTableView
一个简单的工具,旨在简化无限滚动UITableView实现,模仿Facebook或Twitter新闻流等行为。
使用方法
假设您有一个包含UITableView
的基础 UITableViewController
。
class MyViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
}
无限滚动代理
首先,您需要创建一个符合 SRLoadMoreOnScrollTableViewDelegate
的类型。第一步,添加预期内容类型。例如,假设我们的表格视图将包含 String
对象。
struct MyLoadMoreOnScrollTableViewDelegate: SRLoadMoreOnScrollTableViewDelegate {
typealias ContentType = String
}
让编译器再次抱怨,并添加剩余必要的协议存根
var className: String {
return "MyClassName" // if you're targeting a specific class name, for example with Parse API
}
func displayedResults(fromUnorderedResults unorderedResults: [String]) -> [String] {
return unorderedResults.sorted() // how to sort the results in the table view
}
var recordsPerRequest: Int {
return 10 // you will fetch the results 10 by 10
}
func resultCell(forTableView tableView: UITableView, andObject object: String, atIndexPath indexPath: IndexPath) -> UITableViewCell {
// here is the expected result cell
let cell = tableView.dequeueReusableCell(withIdentifier: MyCell.identifier, for: indexPath)
cell.textLabel?.text = object
return cell
}
func didSelect(object: String) {
print("did tap: \(object)")
}
var shouldCacheData: Bool {
return true // if should cache data when fetching data from network
}
var shouldShowFailedCell: Bool {
return true // if you want to show a supplementary cell after the list if an error occurs
}
等等。还有许多其他可选的协议存根可以帮助您根据需要自定义列表。
自定义请求类型
您还需要创建一个定义如何从网络获取数据的类型。如果您使用特定的API,则可以编写一个文件,并在应用程序中的每个列表中重用它。
因此,让我们创建一个符合 SRLoadMoreOnScrollTableViewRequestType
类型的类型。您还需要声明一个 ContentType
struct MyLoadMoreOnScrollTableViewRequestType: SRLoadMoreOnScrollTableViewRequestType {
typealias ContentType = String
}
然后,添加两个预期的函数
func load<Delegate>(delegate: Delegate, skipping: Int, cachingData: Bool, _ completion: @escaping ([String]?, Error?) -> ()) where Delegate : SRLoadMoreOnScrollTableViewDelegate {
//1. create a query, depending on your API, from your delegate's className, parameters, sorting etc...
let query = MyQuery(...)
//2. then call your query
query.findInBackground { (result, error) in
if let result = result {
completion(result, nil)
} else if let error = error {
completion(nil, error)
}
}
}
func loadFromCache<Delegate>(delegate: Delegate, _ completion: @escaping ([String]?, Error?) -> ()) where Delegate : SRLoadMoreOnScrollTableViewDelegate {
// This function is only needed if you want to handle cache data.
// Then update the query accordingly, for example with Parse API, you would add this line :
// query.fromLocalDatastore()
}
返回您的视图控制器
现在您已经定义了自己的代理和请求类型,只需使用它们来创建您的表格视图的管理器即可
var manager: SRLoadMoreOnScrollTableViewManager<MyLoadMoreOnScrollTableViewDelegate, MyLoadMoreOnScrollTableViewRequestType>?
在 viewDidLoad()
函数中实例化它
override func viewDidLoad() {
super.viewDidLoad()
self.manager = SRLoadMoreOnScrollTableViewManager.setupAndStart(inTableView: self.tableView,
withLoadMoreOnScrollDelegate: MyLoadMoreOnScrollTableViewDelegate(),
andRequestType: MyLoadMoreOnScrollTableViewRequestType())
}
并且... 这就是全部了!
编码愉快!