What is a good way to display the counter at the bottom of the table when the user scrolls, but the table cells are browsed slowly?

I have a UITableView that displays some content. When the user scrolls up, the cells below do not always load immediately. This creates a white space at the bottom of the table. I would like to show the spinner in white space and disappear when the content is loaded, but I'm not sure how to do it. What is a good way to implement something like this in fast?

I'm new to coding and iOS, please forgive me if the question is unclear or the answer is obvious.

Example screenshot:

sample image for desired behavior

+4
source share
1 answer

I think this is good for you.

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: .red)
        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
    }
}

tableFooterViewshould be hidden when loading data. when the above function doesn't work, so you may prefer this link.

+4
source

Source: https://habr.com/ru/post/1682460/


All Articles