我是 RxSwift 的新手,我想知道如何能够“被动地”使用 UIRefreshControl 和 UITableView 而不是创建目标的常规方式,并手动调用beginRefreshing()
and endRefreshing()
。
例如,假设我正在从 API 加载一些字符串:
class TableViewController: UITableViewController {
var data : [String] = []
let db = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl = UIRefreshControl()
//I don't want to use
//refreshControl?.addTarget(self, action: #selector(getData), forControlEvents: .ValueChanged)
//Do something to refreshControl.rx_refreshing?
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let str = data[indexPath.row]
cell.textLabel?.text = str
return cell
}
//MARK: - Requests
private func getData() {
let myData = MyAPI.getData() //Returns Observable<[String]>
myData
.subscribe({ [weak self] (event) in
switch event {
case .Next(let strings):
self?.data = strings
self?.tableView.reloadData()
break
case .Error(let err):
print(err)
break
case .Completed:
break
}
// self?.refreshControl?.endRefreshing()
})
.addDisposableTo(db)
}
}
MyAPI
发送对某些字符串值的请求,如何绑定refreshControl
到调用getData()
并在完成(或出错)网络请求时停止刷新?我需要绑定到refreshControl.rx_refreshing吗?