加载时将ActivityIndi cator添加到UITableView的底部

问题描述 投票:11回答:4

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

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

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

谢谢 ;)

ios uitableview swift3 uiactivityindicatorview
4个回答
47
投票

添加此功能

 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应该在数据加载时隐藏。


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
投票

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

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


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
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.