一旦没有更多数据要加载,如何隐藏活动指示器?

问题描述 投票:0回答:1

我正在从Firestore分页数据,并且能够使它正常工作。

这里是分页查询:

if restaurantArray.isEmpty {
                query = db.collection("Restaurant_Data").limit(to: 4)
            } else {
                query = db.collection("Restaurant_Data").start(afterDocument: lastDocument!).limit(to: 4)
            }

query.getDocuments { (querySnapshot, err) in
            if let err = err {
                print("\(err.localizedDescription)")
            } else if querySnapshot!.isEmpty {
                self.fetchMore = false
                return
            } else {
                if (querySnapshot!.isEmpty == false) {
                    let allQueriedRestaurants = querySnapshot!.documents.compactMap { (document) -> Restaurant in (Restaurant(dictionary: document.data(), id: document.documentID)!)}
                    guard let location = self.currentLocation else { return }
                    self.restaurantArray.append(contentsOf: self.applicableRestaurants(allQueriedRestaurants: allQueriedRestaurants, location: location))
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
                        self.tableView.reloadData()
                        self.fetchMore = false
                    })
                    self.lastDocument = querySnapshot!.documents.last
                    }
                }
            }

当用户向上拖动表格视图时将触发分页:

override func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let off = scrollView.contentOffset.y
        let off1 = scrollView.contentSize.height

        if off > off1 - scrollView.frame.height * leadingScreensForBatching{
            if !fetchMore { // excluded reachedEnd Bool
                if let location = self.currentLocation {
                    queryGenerator(searched: searchController.isActive, queryString: searchController.searchBar.text!.lowercased(), location: location)
                }
            }
        }
    }

我还在底部添加了一个活动指示器,该指示器可以正常工作。这是该代码:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let lastSectionIndex = tableView.numberOfSections - 1
        let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
        if indexPath.section ==  lastSectionIndex && indexPath.row == lastRowIndex {
            // print("this is the last cell")
            let spinner = UIActivityIndicatorView(style: .medium)
            spinner.startAnimating()
            spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))

            tableView.tableFooterView = spinner
            tableView.tableFooterView?.isHidden = false
        }
    }

但是,一旦到达表格视图的底部并且没有更多可用数据,该指示器仍在显示,我不知道如何使其不显示。

我尝试使用变量来检查Firestory查询是否完成,但是我的实现可能是错误的,所以我无法使其正常工作。

swift google-cloud-firestore pagination tableview uiactivityindicatorview
1个回答
0
投票

如果知道没有可用数据的那一刻,请将tableView页脚视图设置为nil或hidden = true,请参见下文

tableView.tableFooterView?.isHidden = true

tableView.tableFooterView = nil

在致电self.tableView.reloadData()之前

如果不知道,可以实现布尔值[[reachedEnd,当为true时,您将知道没有更多数据。

© www.soinside.com 2019 - 2024. All rights reserved.