【问题标题】:Start and Stop indicatorView and put it in centre tableView in swift快速启动和停止indicatorView并将其放在中心tableView中
【发布时间】:2018-11-23 07:50:48
【问题描述】:

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

【问题讨论】:

  • 每当从 API 成功加载数据时尝试 self.tableView.tableFooterView?.isHidden = true
  • 在哪里调用api方法?
  • 是的。在 API 响应方法里面。
  • 用调用 api 更新了我的问题,告诉我在哪里调用这个方法
  • DispatchQueue.main.async { self.reloadData() self.yourTableView.tableFooterView?.isHidden = true }

标签: ios swift uitableview uiactivityindicatorview


【解决方案1】:

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

    }
   ...

【讨论】:

  • 感谢您的回答,我会尝试的,但我想要它在中心我怎么能做到这一点?
  • var spinner: UIActivityIndi​​catorView!覆盖 func viewDidLoad() { self.spinner = UIActivityIndi​​catorView(.... 并将微调器添加到 self.view
【解决方案2】:

对于 center 的你们中的一些人,您可以按照以下步骤操作。您无需为微调器提供高度和宽度。

    let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray)
    spinner.activityIndicatorViewStyle = .whiteLarge
    spinner.color = .red
    spinner.startAnimating()

    let topBarHeight = UIApplication.shared.statusBarFrame.size.height +
        (self.navigationController?.navigationBar.frame.height ?? 0.0)

    spinner.center=CGPointMake(self.tableView.center.x, self.tableView.center.y- topBarHeight);

【讨论】:

  • 出现错误使用未解析的标识符statusBarHeightnavigationHeight
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多