【问题标题】:How do I hide the activity indicator once there is no more data to load?一旦没有更多数据要加载,如何隐藏活动指示器?
【发布时间】:2020-08-26 01:05:36
【问题描述】:

我正在对来自 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 查询是否已完成,但我的实现可能是错误的,所以我无法让它工作。

【问题讨论】:

  • 如果有数据要获取,则应在scrollViewDidScroll方法中添加加载指示器,并在数据响应完成开始时隐藏它。

标签: swift google-cloud-firestore pagination tableview uiactivityindicatorview


【解决方案1】:

如果您知道没有更多可用数据的时刻,则将 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
                }

        }
    } 

【讨论】:

  • 感谢您的建议。有没有办法确定是否没有更多数据,即集合或查询中没有更多文档?
猜你喜欢
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 2017-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
相关资源
最近更新 更多