DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom
  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Fixing Common Oracle Database Problems

Trending

  • Using Python Libraries in Java
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  • Integration Isn’t a Task — It’s an Architectural Discipline
  1. DZone
  2. Data Engineering
  3. Data
  4. Displaying an Activity Indicator while Loading Table View Data

Displaying an Activity Indicator while Loading Table View Data

Let your users know about long running tasks with activity indicators.

By 
Greg Brown user avatar
Greg Brown
·
Dec. 22, 15 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
48.8K Views

Join the DZone community and get the full member experience.

Join For Free

iOS applications often need to retrieve data from a remote source such as a web service. This data is commonly loaded in the background to avoid blocking the main UI thread, causing the application to appear unresponsive.

While the data is being loaded, an application typically displays an activity indicator view to inform the user that something is happening. Often, this is done by dynamically adding an instance of UIActivityIndicatorView as a subview of either the current view or one of the view's ancestors and making the indicator active. However, if the view is a table or collection view, there is another option: the activity indicator can be set as the view's background view and shown or hidden as needed.

For example, the following code snippet shows a partial implementation of a simple table view controller. The table's data is provided by an array of strings stored in the rows property. In viewDidLoad, the controller creates the activity indicator view and sets it as the table view's background view. Because table views in the default "plain" style show separator lines even when the table is empty, the controller also sets the table view's separator style to None so the lines don't interfere with the activity indicator when it is visible:

class ViewController: UITableViewController {
    weak var activityIndicatorView: UIActivityIndicatorView!

    var rows: [String]! = nil

    let cellIdentifier = "cell"

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "Activity Indicator Example"

        let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)

        tableView.backgroundView = activityIndicatorView
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None

        self.activityIndicatorView = activityIndicatorView

        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
    }

    ...
}

The data is loaded in viewWillAppear:. The controller simulates a web service call by sleeping for three seconds in the background, then populating the rows array and reloading the table data on the main thread. The activity indicator is enabled while the background operation is executing, and is disabled when the operation is complete:

class ViewController: UITableViewController {
    ...

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        if (rows == nil) {
            activityIndicatorView.startAnimating()

            AppDelegate.operationQueue.addOperationWithBlock() {
                NSThread.sleepForTimeInterval(3)

                NSOperationQueue.mainQueue().addOperationWithBlock() {
                    self.activityIndicatorView.stopAnimating()

                    self.rows = ["One", "Two", "Three", "Four", "Five"]

                    self.tableView.separatorStyle = UITableViewCellSeparatorStyle.SingleLine
                    self.tableView.reloadData()
                }
            }
        }
    }

    ...
}

Image title

Finally, the controller overrides numberOfSectionsInTableView:, tableView: numberOfRowsInSection:, and tableView:cellForRowAtIndexPath: to provide the table content. It reports zero sections while the data is being loaded, and one section once the data is available:

class ViewController: UITableViewController {
    ...

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return (rows == nil) ? 0 : 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return rows.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)!

        cell.textLabel!.text = rows[indexPath.row]

        return cell
    }
}

Image title

Data (computing) Database Indicator (metadata)

Opinions expressed by DZone contributors are their own.

Related

  • Using Technical Analysis Indicators to Send Buy-or-Sell Trading Signals to a Chatroom
  • Why Database Migrations Take Months and How to Speed Them Up
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Fixing Common Oracle Database Problems

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: