代码之家  ›  专栏  ›  技术社区  ›  Wings

启动和停止指示灯视图,并将其放在swift的中央表格视图中

  •  0
  • Wings  · 技术社区  · 7 年前

    我正在使用底部的旋转器 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()
            }
        }
    }
    

    2 回复  |  直到 7 年前
        1
  •  1
  •   Nguyen Hoan    7 年前

    在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-func:

    ...
        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
            }
    
        }
       ...
    
        2
  •  0
  •   SachinVsSachin    7 年前

    为了 中心 您可以按照以下步骤对tableview进行微调。您不需要为微调器提供高度和宽度。

        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);