在加载

时间:2017-02-25 14:33:19

标签: ios uitableview swift3 uiactivityindicatorview

我想在显示最后一个单元格时在我的UITableView底部添加一个ActivityIndi​​cator,而我正在获取更多数据,然后在获取数据时隐藏它。

所以我滚动到底部 - >最后一行显示 - >在获取数据时,微调器开始旋转 - >获取数据,隐藏微调器 - >新数据添加到tableview。

关于如何实现这一目标的任何提示?

谢谢;)

6 个答案:

答案 0 :(得分:46)

添加此功能

 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(activityIndicatorStyle: .gray)
        spinner.startAnimating()
        spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))

        self.tableview.tableFooterView = spinner
        self.tableview.tableFooterView?.isHidden = false
    }
}

和tableFooterView应在数据加载时隐藏。

答案 1 :(得分:9)

雨燕4

 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: .gray)
        spinner.startAnimating()
        spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))

        self.tableview.tableFooterView = spinner
        self.tableview.tableFooterView?.isHidden = false
    }
}

答案 2 :(得分:7)

更新2020 Swift 5,Xcode 11

我使用了@Ayaz Akbar的解决方案,它的工作原理很吸引人。它只是需要一些改进。这是我根据需要采用的方式。

如果没有其他要加载的项目,则删除加载指示器

由于每次到项目末尾都要添加加载指示器,所以有时您将不再向表视图数据源添加新项目。此时,您需要卸下加载指示器。我使用的是自定义UITableViewDataSource,每次接收到新数据时都会调用其完成处理程序。我还保留一个var newCount来跟踪新项目的数量。所以这是我在视图控制器中隐藏活动指示器的操作:

private lazy var dataSource: CustomTableViewDataSource = {
    return CustomTableViewDataSource(query: CustomQuery) { [unowned self] (result) in
    // For the initial load I needed a standard activity indicator in the center of the screen
    self.stopMainActivityIndicatorIfNeeded()

    switch result {
    case .success(()):
        if self.dataSource.newCount > 0 {
            self.tableView.reloadData()
        } else {
            // Hide the tableFooterView, respectively the activity indicator
            self.tableView.tableFooterView = nil
        }
    case .failure(let error):
        self.showError(error)
    }
    }
}()

这是我更新的UITableView委托方法:

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 && dataSource.newCount > 0 {
        let spinner = UIActivityIndicatorView(style: .gray)
        spinner.frame = CGRect(x: 0.0, y: 0.0, width: tableView.bounds.width, height: 70)
        spinner.startAnimating()
        tableView.tableFooterView = spinner
    }
}

答案 3 :(得分:2)

一种方法是添加一个带有UIActivityIndi​​catorView的自定义单元格。你可以把它放在一个单独的部分。有很多方法可以做到这一点。

或者你看看这个:https://github.com/evnaz/ENFooterActivityIndicatorView

答案 4 :(得分:0)

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    let total = (self.array.count )
    if (indexPath.item + 1) == (self.array.count ){
        self.limit = Int(self.limit+20) // table item render limit
        if total < limit {
            let spinner = UIActivityIndicatorView(style: .gray)
            spinner.startAnimating()
            spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44))

            self.myTableView.tableFooterView = spinner
            self.myTableView.tableFooterView?.isHidden = false
        }
    }
}

答案 5 :(得分:0)

您可以将子视图添加到 UITableView 并在用户滚动到底部时显示它。

我不得不在多个 UIScrollViews 上实现相同的东西,最终为它创建了一个 DSL

还创建了一个 pod
如果需要,您可以查看源代码或使用 pod。

https://github.com/salmaanahmed/PullToRefreshDSL