ios - 快速启动和停止indicatorView并将其放在中心tableView中
问题描述
我在底部使用微调器tableView
来在数据重新加载时显示微调器。
var sneakers: [sneakerModel] = []
我将所有数据存储在运动鞋中并返回计数numberOfRowsInSection
方法。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.sneakers.count
}
现在我正在使用willDisplay
方法在底部显示微调器。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.row == self.sneakers.count - 1 && !didPageEnd {
pageNo += 1
getSneakerSearch()
}
if (indexPath.row == self.sneakers.count) {
self.tableView.tableFooterView?.isHidden = true
} else {
if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex {
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.activityIndicatorViewStyle = .whiteLarge
spinner.color = .red
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(5), width: tableView.bounds.width, height: CGFloat(44))
self.tableView.tableFooterView = spinner
self.tableView.tableFooterView?.isHidden = false
}
}
}
但它不起作用。没有更多数据加载时如何停止微调器。请帮忙?
从 api 获取数据。
func getSneakerSearch() {
let param: [String:Any] = [ "search_term" : searchText ?? "","page": pageNo, "token": commonClass.sharedInstance.userToken ?? ""]
if self.pageNo == 0{
commonClass.sharedInstance.startLoading()
}
Alamofire.request(Constants.API.url("search"), method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: nil).responseJSON {
(response:DataResponse<Any>) in
commonClass.sharedInstance.stopLoading()
guard let json = response.result.value as? [String:Any] else {return}
printD(json)
guard let status = json["status"] as? Int else {return}
printD(status)
if status == 1 {
guard let data = json["data"] as? [[String:Any]] else { return}
printD(data)
if self.pageNo == 0 {
self.sneakers.removeAll()
}
for item in data {
guard let description = item["description"] as? String else { return}
printD(description)
self.sneakers.append(sneakerModel(response: item))
}
self.didPageEnd = data.count < limitPerPage
}
DispatchQueue.main.async {
self.reloadData()
}
}
}
解决方案
在 viewdidload 中创建 loadingview
override func viewDidLoad() {
super.viewDidLoad()
let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
spinner.activityIndicatorViewStyle = .whiteLarge
spinner.color = .red
spinner.startAnimating()
spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(5), width: tableView.bounds.width, height: CGFloat(44))
self.tableView.tableFooterView = spinner
self.tableView.tableFooterView?.isHidden = true
}
并更新willDisplay cell
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let lastSectionIndex = tableView.numberOfSections - 1
let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
if indexPath.row == self.sneakers.count - 1 && !didPageEnd {
pageNo += 1
getSneakerSearch()
}
}
更新 getSnackerSearch 函数:
...
if self.pageNo == 0{
commonClass.sharedInstance.startLoading()
}else{
self.tableView.tableFooterView?.isHidden = false
}
Alamofire.request(Constants.API.url("search"), method: .post, parameters: param, encoding: URLEncoding.httpBody, headers: nil).responseJSON {
(response:DataResponse<Any>) in
if self.pageNo == 0{
commonClass.sharedInstance.stopLoading()
}else{
self.tableView.tableFooterView?.isHidden = true
}
}
...
推荐阅读
- python - 带有空值的 Pandas 三个月移动平均线
- c# - 了解如何从 XDocument.SaveAsync 方法获取任务反馈
- jquery - JQuery - setTimeout 没有延迟
- typescript - 默认导出的 Typescriptp 语法
- android - 通知栏未在android中显示
- android - Is it possible to use PWAS in Android TV?
- r - 如何使用 Dplyr 对前四列和后四列进行分组?
- r - 我在对数据集设置子集时遇到问题
- javascript - 背景图像 url() 不起作用,但带有 src 的图像标签可以
- javascript - js中的问号是什么意思?