ios - 启动和停止 indicatorView 并 swift 将其放在中心 tableView 中

标签 ios swift uitableview uiactivityindicatorview

我在 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
        }

    }
   ...

关于ios - 启动和停止 indicatorView 并 swift 将其放在中心 tableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53442594/

相关文章:

ios - 使用 .long 或 .full 的日期格式不会导致小时/分钟/天/秒

ios - 在Swift中解析objectID

ios - 在 UITableView 的 Storyboard中创建 headerView 原型(prototype)并使用 "resuerIdentifier"将其出列

ios - 以编程方式激活 UISearchBar 阻止用户与其交互

uitableview - 如何使 UITableViewCell 显示为禁用?

iOS TabBarController 背景颜色

ios - 在 iOS 中从 Google Analytics 迁移到 Firebase

ios - SKLabelNode 文本消失使用 iOS 7.1

ios - 位置坐标无效

c++ - Swift - 使用 Objective-C 包装器调用 C++ 文件 - 传递 Int 值