一旦没有更多数据可加载,如何隐藏 activity 指示器?

How do I hide the activity indicator once there is no more data to load?

我正在对来自 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
                    }
                }
            }

当用户向上拖动 table 视图时触发分页:

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

我还在底部添加了一个 activity 指标,它按预期工作。这是相关代码:

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

但是,一旦到达 table 视图的底部并且没有更多数据可用,指标仍在显示,我不知道如何让它不显示。

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

如果您知道没有更多可用数据的时刻,您可以将 tableView 页脚视图设置为 nil 或 hidden = true,见下文

tableView.tableFooterView?.isHidden = true

tableView.tableFooterView = nil

在你打电话之前 self.tableView.reloadData()

如果你不知道,请看下面的代码

query.getDocuments { (querySnapshot, err) in

        if let err = err {
            print("\(err.localizedDescription)")
        } else if querySnapshot!.isEmpty {
            // Here you know when there is no more data
            self.fetchMore = false
            self.tableView.tableFooterView = nil // or self.tableView.tableFooterView?.isHidden = true
            self.tableView.reloadData()
            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
                }

        }
    }