Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 启动和停止指示灯视图,并将其置于swift中的中间表格视图中_Ios_Swift_Uitableview_Uiactivityindicatorview - Fatal编程技术网

Ios 启动和停止指示灯视图,并将其置于swift中的中间表格视图中

Ios 启动和停止指示灯视图,并将其置于swift中的中间表格视图中,ios,swift,uitableview,uiactivityindicatorview,Ios,Swift,Uitableview,Uiactivityindicatorview,我使用tableView底部的微调器在数据重新加载时显示微调器 var sneakers: [sneakerModel] = [] 我将所有数据存储在运动鞋中,并返回numberOfRowsInSection方法的计数 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.sneakers.count } 现在我使用willDisplay

我使用
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()
        }
    }
}
func getSneakerSearch(){
let参数:[String:Any]=[“搜索词”:searchText??”,“页面”:pageNo,“令牌”:commonClass.sharedInstance.userToken??”]
如果self.pageNo==0{
commonClass.sharedInstance.StartingLoading()
}
请求(Constants.API.url(“搜索”),方法:.post,参数:param,编码:URLEncoding.httpBody,头:nil)。responseJSON{
(响应:DataResponse)在
commonClass.sharedInstance.stopLoading()的
guard let json=response.result.value为?[String:Any]else{return}
printD(json)
guard let status=json[“status”]as?Int else{return}
打印D(状态)
如果状态==1{
guard let data=json[“data”]as?[[String:Any]]else{return}
打印D(数据)
如果self.pageNo==0{
self.sneakers.removeAll()
}
对于数据中的项{
guard let description=item[“description”]作为字符串else{return}
打印D(说明)
self.sneakers.append(sneakerModel(响应:项目))
}
self.didpagend=data.count

在viewdidload中创建加载视图

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
}
并更新
将显示单元格

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
        }

    }
   ...
。。。
如果self.pageNo==0{
commonClass.sharedInstance.StartingLoading()
}否则{
self.tableView.tableFooterView?.ishiden=false
}
请求(Constants.API.url(“搜索”),方法:.post,参数:param,编码:URLEncoding.httpBody,头:nil)。responseJSON{
(响应:DataResponse)在
如果self.pageNo==0{
commonClass.sharedInstance.stopLoading()的
}否则{
self.tableView.tableFooterView?.ishiden=true
}
}
...

对于将微调器旋转到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);

每当从API成功加载数据时,请尝试self.tableView.tableFooterView?.ishiden=true。调用API方法的位置?是。在内部的API响应方法中。使用调用API更新了我的问题。请告诉我在何处调用此方法DispatchQueue.main.async{self.reloadData()self.yourTableView.tableFooterView?.isHidden=true}谢谢你给我答案,我会尝试一下,但我想再多尝试一次,我如何才能做到这一点呢?var spinner:UIActivityIndicatorView!重写func viewDidLoad(){self.spinner=UIActivityIndicatorView(..)并将微调器添加到self.viewgetting错误使用未解析标识符
statusBarHeight
navigationHeight